Дарт: загрузить файл .jpg на Google Drive - PullRequest
0 голосов
/ 09 мая 2019

Я пытаюсь загрузить файл .jpg на Google Drive в Dart:

final access_token = '...';

// sfilePath is "/storage/emulated/0/test/"
// sfileName is "test"

Uri uri = Uri.parse('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');

http.MultipartRequest request = new http.MultipartRequest('POST', uri);
request.headers["Authorization"] = "Bearer $access_token";
request.fields['metadata'] =  "{name : " + sfileName + '.jpg' +  "};type=application/json;charset=UTF-8";
request.files.add(http.MultipartFile.fromString('metadata',
                                  JSON.jsonEncode({'name': sfilePath + sfileName + '.jpg'}),
                                  contentType: MediaType('application', 'json'),
                  ));

http.StreamedResponse response = await request.send();
print('>>>>>>>>>>>>>>>>>>>>>' + response.statusCode.toString());

Я получаю неправильный запрос кода состояния: 400 Я видел сообщение о том же isue: Dart: httpотправить сообщение на Google Drive

Что мне не хватает?Спасибо

1 Ответ

0 голосов
/ 09 мая 2019

Может быть, вы могли бы использовать пакет googleapis.Это может быть проще в использовании.

Добавьте эти зависимости в pubspec.yaml

dependencies:
  googleapis:
  googleapis_auth:

И используйте библиотеку googleapis:

import 'package:googleapis/drive/v3.dart';
import 'package:googleapis_auth/auth_io.dart';
import 'dart:io' as io;

main() async {
  AuthClient client = await clientViaServiceAccount(
      ServiceAccountCredentials.fromJson({/* here the credentials */}),
      ['https://www.googleapis.com/auth/drive']);

  var driveApi = DriveApi(client);

  var fileToUpload = io.File('my_file.png');

  await driveApi.files.create(File()..name = 'my_file.png',
      uploadMedia: Media(fileToUpload.openRead(), fileToUpload.lengthSync()));
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...