Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何使用Flatter访问Google Drive appdata文件夹文件?_Flutter_Google Api_Google Drive Api - Fatal编程技术网

Flutter 如何使用Flatter访问Google Drive appdata文件夹文件?

Flutter 如何使用Flatter访问Google Drive appdata文件夹文件?,flutter,google-api,google-drive-api,Flutter,Google Api,Google Drive Api,我有一个非常古老的Android项目,我已经很久没有接触过了。 它将一些用户数据存储在用户Google Drive appdata文件夹中。 现在,我正在将应用程序更新为Flutter版本,因为googledriveapi已经被弃用,没有Flutter的插件,我相信我现在需要使用googleapi。但我找不到太多关于我的颤振问题。 我马上就要用谷歌登录了:^4.0.7 GoogleSignIn _googleSignIn = GoogleSignIn( scopes: [ '

我有一个非常古老的Android项目,我已经很久没有接触过了。 它将一些用户数据存储在用户Google Drive appdata文件夹中。 现在,我正在将应用程序更新为Flutter版本,因为googledriveapi已经被弃用,没有Flutter的插件,我相信我现在需要使用googleapi。但我找不到太多关于我的颤振问题。 我马上就要用谷歌登录了:^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文件夹中的文件

EDIT1:这个答案很有帮助,我成功地获得了httpClient,但我仍然无法获得appdata文件夹及其文件


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

以下内容对我很有用,(使用包获取
发布

身份验证令牌

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

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

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

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'];

谢谢,我试试看。但是你是如何得到$authToken的?你又是如何实现“get”和“post”方法的?我得到了一个合理的短语:禁止使用你的搜索代码。我需要其他查询参数吗?或者应该这样做吗?嗯,它似乎试图获得错误应用程序的许可。它试图获取我的颤振应用程序名,而不是我的Android应用程序名。是否可以在请求上设置应用程序名称?因为我不知道还能改变什么。pubspeck.yaml不接受我的应用程序名称,因为它包含空格。Android文件似乎设置正确。是的,问题是它使用的是颤振应用程序名而不是Android的来获取应用程序文件夹。固定的。
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'];
final headers = { 'Authorization': 'Bearer $authToken' };
final url = 'https://www.googleapis.com/drive/v3/files/$fileId?alt=media';
final response = await get(url, headers: headers);