如何使用Flutter访问Google云端硬盘的appdata文件夹文件? [英] How to access Google Drive appdata folder file with Flutter?

查看:209
本文介绍了如何使用Flutter访问Google云端硬盘的appdata文件夹文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很老的Android项目,很长一段时间都没有碰过. 它将一些用户数据存储在用户的Google云端硬盘appdata文件夹中. 现在,我将应用程序更新为Flutter版本,并且由于不推荐使用Google Drive API,因此没有Flutter插件,我认为我现在需要使用googleapi.但是对于扑打的问题,我找不到太多的东西. 我到了用google_sign_in登录的地步:^ 4.0.7

I have this very old Android project that I haven't touched for a long while. It stores some user data on the users Google Drive appdata folder. Now I'm updating the app to a Flutter version and since Google Drive API is being deprecated, there's no plugin for Flutter, I believe I need to use googleapi for that now. But I can't find much regarding my problem for flutter. I got to the point of signing in with 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云端硬盘上appdata文件夹中的文件?

That works fine, but I got stuck there. How can I go from there and read a file inside the appdata folder on the user's Google Drive?

这个答案很有帮助,我设法获得了httpClient,但是我仍然对如何获取appdata文件夹及其文件仍然有所了解.

This answer helped, I managed to get the httpClient but I'm still stuck on how to get the appdata folder and its files How to use Google API in flutter?

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

It seems that googleapi doesn't support the appfolder since Google MAY be deprecating it in the future (seems like they already did it) in order to force us to pay for storage using firebase. Ok, fine, but how do I migrate it if I can't access the folder via googleapi? If I reset my app now and my users lose all their data I'll lose the few users I have...

推荐答案

以下内容对我有用(使用 http 包(用于getpost)

The following works for me, (using the http package for get and post)

身份验证令牌

您可以从signIn返回的帐户中检索auth令牌.

You can retrieve the auth token from the accoutn returned by 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 queryParameters并将其设置为appDataFolder.该文档在这方面有点误导.

To search files in the AppData directory you need to add the spaces queryParameters and set it to appDataFolder. The documentation is kind of misleading on that part.

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.

To upload a file you need to set the parents to appDataFolder property of the body for the initial upload request. To download the file you only need the 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进行检索(见上文).

To download the file you only need to know the fileId, if you don't know it you need to use the search API to retrieve it (see above).

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

这篇关于如何使用Flutter访问Google云端硬盘的appdata文件夹文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆