【问题标题】:How to access Google Drive appdata folder file with Flutter?如何使用 Flutter 访问 Google Drive appdata 文件夹文件?
【发布时间】:2019-09-24 03:36:30
【问题描述】:

我有一个很久没有碰过的非常古老的 Android 项目。 它将一些用户数据存储在用户 Google Drive appdata 文件夹中。 现在我正在将应用程序更新为 Flutter 版本,并且由于 Google Drive API 已被弃用,因此没有 Flutter 插件,我相信我现在需要使用 googleapi。但我找不到太多关于颤振的问题。 我到了用 google_sign_in 登录的地步:^4.0.7

GoogleSignIn _googleSignIn = GoogleSignIn(
    scopes: [
      'email',
      'https://www.googleapis.com/auth/drive.appdata',
      'https://www.googleapis.com/auth/drive.file',
    ],
  );
  try {
    GoogleSignInAccount account = await _googleSignIn.signIn();
  } catch (error) {
    print(error);
  }

这很好,但我被困在那里。如何从那里读取用户 Google Drive 上 appdata 文件夹中的文件?

EDIT1:这个答案有帮助,我设法获得了 httpClient,但我仍然坚持如何获得 appdata 文件夹及其文件How to use Google API in flutter?

googleapi 似乎不支持 appfolder,因为 Google 将来可能会弃用它(似乎他们已经这样做了),以迫使我们使用 firebase 支付存储费用。好的,很好,但是如果我无法通过 googleapi 访问该文件夹,我该如何迁移它?如果我现在重置我的应用程序并且我的用户丢失了他们的所有数据,我将失去我拥有的少数用户......

【问题讨论】:

    标签: flutter google-api google-drive-api


    【解决方案1】:

    以下对我有用,(使用getposthttp 包)

    身份验证令牌

    您可以从signIn返回的帐户中检索身份验证令牌。

    Future<String> _getAuthToken() async {
      final account = await sign_in_options.signIn();
      if (account == null) {
        return null;
      }
      final authentication = await account.authentication;
      return authentication.accessToken;
    }
    

    搜索

    要在 AppData 目录中搜索文件,您需要添加 spaces 查询参数并将其设置为 appDataFolder。文档在这方面有点误导。

    final Map<String, String> queryParameters = {
      'spaces': 'appDataFolder',
      // more query parameters
    };
    final headers = { 'Authorization': 'Bearer $authToken' };
    final uri = Uri.https('www.googleapis.com', '/drive/v3/files', queryParameters);
    final response = await get(uri, headers: headers);
    

    上传

    要上传文件,您需要为初始上传请求将正文的parents 设置为appDataFolder 属性。要下载文件,您只需要 fileId。

    final headers = { 'Authorization': 'Bearer $authToken' };
    final initialQueryParameters = { 'uploadType': 'resumable' };
    final Map<String, dynamic> metaData = { 
      'name': fileName,
      'parents': ['appDataFolder ']
    };
    final initiateUri = Uri.https('www.googleapis.com', '/upload/drive/v3/files', initialQueryParameters);
    final initiateResponse = await post(initiateUri, headers: headers, body: json.encode(metaData));
    final location = initiateResponse.headers['location'];
    

    下载

    要下载文件你只需要知道fileId,如果你不知道你需要使用搜索API来检索它(见上文)。

    final headers = { 'Authorization': 'Bearer $authToken' };
    final url = 'https://www.googleapis.com/drive/v3/files/$fileId?alt=media';
    final response = await get(url, headers: headers);
    

    【讨论】:

    • 谢谢,我会试试的。但是如何获得 $authToken?
    • 还有你是如何实现“get”和“post”方法的?
    • 我得到了 reasonPhrase: Forbidden with your search code。我需要其他查询参数吗?还是应该按原样工作?
    • 嗯,它似乎试图获得错误应用的许可。它试图获取我的颤振应用名称而不是我的 Android 应用名称。是否可以在请求中设置应用名称?因为我不知道还有什么可以改变的。 pubspeck.yaml 不会接受我的应用名称,因为它包含空格。 Android 文件似乎设置正确。
    • 是的,问题在于它使用颤振应用程序名称而不是 Android 来获取应用程序文件夹。固定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多