【发布时间】:2023-03-31 07:40:01
【问题描述】:
我正在尝试将在我的应用程序中拍摄的一些照片上传到 Google Drive。
我创建了一个项目和一个服务帐户密钥,并启用了 Google Drive API。
我的代码 sn-p 如下所示:
// sfilePath = "/storage/emulated/0/Xyz";
// sfileName = "PIC_17";
final accountCredentials = new ServiceAccountCredentials.fromJson({
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "...@...gserviceaccount.com",
"client_id": "...apps.googleusercontent.com",
"type": "service_account"
});
AuthClient client = await clientViaServiceAccount(
accountCredentials,
['https://www.googleapis.com/auth/drive']);
var driveApi = ga.DriveApi(client);
var fileToUpload = File(sfilePath + sfileName + '.jpg');
var response = await driveApi.files.create(ga.File()..name = 'my_file.jpg',
uploadMedia: ga.Media(fileToUpload.openRead(), fileToUpload.lengthSync()));
print(response.toJson());
运行程序后
print(response.toJson())
结果:
{id: ..., kind: drive#file, mimeType: image/jpeg, name: my_file.jpg}
我看到 Dashboard 上 Google Drive API 的请求数在一段时间后增加,但我在我的 Google Drive 上找不到 my_file.jpg。
可能是什么问题?我如何测试会发生什么,确切的响应是什么?
我的解决方案来自:Dart: upload .jpg file to google Drive
谢谢, 莫泽斯
【问题讨论】: