Access bLocked:App_Name 请求无效

问题描述 投票:0回答:0

我正在尝试使用 Google Drive API 在我的 Flutter 应用程序中添加 Google 备份选项。当用户单击备份按钮时,我将调用 uploadFileToGoogleDrive() 函数,该函数将文件上传到 Google Drive。我的目标是允许用户通过单击备份按钮将数据备份到 Google Drive。但是,我收到以下错误:

这是我的代码:

class SecureStorage {
  final storage = FlutterSecureStorage();

  //Save Credentials
  Future saveCredentials(AccessToken token, String refreshToken) async {
    print(token.expiry.toIso8601String());
    await storage.write(key: "type", value: token.type);
    await storage.write(key: "data", value: token.data);
    await storage.write(key: "expiry", value: token.expiry.toString());
    await storage.write(key: "refreshToken", value: refreshToken);
  }

  //Get Saved Credentials
  Future<Map<String, dynamic>?> getCredentials() async {
    var result = await storage.readAll();
    if (result.isEmpty) return null;
    return result;
  }

  //Clear Saved Credentials
  Future clear() {
    return storage.deleteAll();
  }
}

const _clientId = "###.apps.googleusercontent.com";
const _scopes = [ga.DriveApi.driveFileScope];


class GoogleDrive {
  final storage = SecureStorage();
  //Get Authenticated Http Client
  Future<http.Client?> getHttpClient() async {
    //Get Credentials
    var credentials = await storage.getCredentials();
    if (credentials == null) {
      try {
        //Needs user authentication
        var authClient = await clientViaUserConsent(
            ClientId(_clientId),_scopes, (url) {
          //Open Url in Browser
          launch(url);
        });
        //Save Credentials
        await storage.saveCredentials(authClient.credentials.accessToken,
            authClient.credentials.refreshToken!);
        print(authClient);
        return authClient;
      } catch (e) {
        print('Error getting user consent: $e');
        // Show a message to the user that the authentication process failed
        return null;
      }
    } else {
      print(credentials["expiry"]);
      //Already authenticated
      return authenticatedClient(
          http.Client(),
          AccessCredentials(
              AccessToken(credentials["type"], credentials["data"],
                  DateTime.tryParse(credentials["expiry"])!),
              credentials["refreshToken"],
              _scopes));

    }
  }



  Future<String?> _getFolderId(ga.DriveApi driveApi) async {
    final mimeType = "application/vnd.google-apps.folder";
    String folderName = "NewFodler";

    try {
      final found = await driveApi.files.list(
        q: "mimeType = '$mimeType' and name = '$folderName'",
        $fields: "files(id, name)",
      );
      final files = found.files;
      if (files == null) {
        print("Sign-in first Error");
        return null;
      }

      // The folder already exists
      if (files.isNotEmpty) {
        return files.first.id;
      }

      // Create a folder
      ga.File folder = ga.File();
      folder.name = folderName;
      folder.mimeType = mimeType;
      final folderCreation = await driveApi.files.create(folder);
      print("Folder ID: ${folderCreation.id}");

      return folderCreation.id;
    } catch (e) {
      print(e);
      return null;
    }
  }


  uploadFileToGoogleDrive(File file) async {
    var client = await getHttpClient();
    var drive = ga.DriveApi(client!);
    String? folderId =  await _getFolderId(drive);
    if(folderId == null){
      print("Sign-in first Error");
    }else {
      ga.File fileToUpload = ga.File();
      fileToUpload.parents = [folderId];
      fileToUpload.name = p.basename(file.absolute.path);
      var response = await drive.files.create(
        fileToUpload,
        uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
      );
      print(response);
    }

  }
}

这是用户点击按钮时的代码:

final appDocumentDir = await getApplicationDocumentsDirectory();
googleDrive.uploadFileToGoogleDrive(File(weekBox.path.toString()));

谁能告诉我如何解决这个错误?任何帮助将不胜感激。

flutter oauth-2.0 google-api google-drive-api google-signin
© www.soinside.com 2019 - 2024. All rights reserved.