1
votes

I'm trying to use for the first time a Google API, Google Drive API, I'm authenticating the user with Google SignIn. I created a ClientId, ClientSecret and Scope after enabling Google Drive API, but I don't know how to use them. I ended up creating a HTTP client using the GoogleSignInAccount's authHeaders.

I won't need to access my files or manage them, I just need to be authenticated because I might need to download big files, using direct links doesn't work since it has a confirmation window for big files that can't be scanned.

When I try do download a public file using it's ID I always get the error below.

class GoogleHttpClient extends IOClient {
  Map<String, String> _headers;

  GoogleHttpClient(this._headers) : super();

  @override
  Future<IOStreamedResponse> send(BaseRequest request) =>
      super.send(request..headers.addAll(_headers));

  @override
  Future<Response> head(Object url, {Map<String, String> headers}) =>
      super.head(url, headers: headers..addAll(_headers));

}

class GDriveHelper {

  // NOT USING
  //static final clientId = "??????.apps.googleusercontent.com";
  //static final clientSecret = "??????";
  //static final scopes = [ga.DriveApi.DriveFileScope];

  static UserController userController = Get.find();

  static Future<void> download(String fileId) async {

    Map<String, String> headers = await userController.googleSignInAccount.authHeaders;

    var client = GoogleHttpClient(headers);
    var drive = ga.DriveApi(client);

    await drive.files.get(fileId).then((file){

      print("FILE: $file");

    });



  }

}

When I call the method I get this error:

await GDriveHelper.download(fileId);
            
DetailedApiRequestError(status: 403, message: Insufficient Permission: Request had insufficient authentication scopes.)
1
In your case, for example, when you authorize the scope again, what result will you obtain? And as other confirmation, when you modified ga.DriveApi.DriveFileScope to ga.DriveApi.DriveScope, what result will you obtain? In that case, please reauthorize the scope again. - Tanaike
I created the variable scope but I am not using it anywhere in the code. I don't know if I have to and where to use these variables (clientId, clientSecret and scopes). - djalmafreestyler
Thank you for replying. I noticed that your question was updated just now. I apologize for this. In your script, you have tested without using the values for authorizing. If my understanding is correct, from your updated question, for example, how about using API key? In that case, the publicly shared file can be downloaded by the simple request like GET https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]. How about this? - Tanaike
Thank you for replying. The client secret is different from the API key. You can see about the API key at the official document. Ref When you got the API key, for example, you can download the publicly shared file with a simple curl command like curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]". - Tanaike
@Tanaike Thank you very much, it worked, I didn't think using that way by url was possible and so easy, could you please answer this question so I can accept your answer? - djalmafreestyler

1 Answers

3
votes

From your question, I could understand that you try to download a publicly shared file. And from our discussions, I confirmed that clientId, clientSecret and scopes are not used. From these situation, as a simple method, I would like to propose to downloading the publicly shared file using the API key. The API key can download the publicly shared file. And the request can be simple as follows.

Sample request:

GET https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]
  • In this method, the files except for Google Docs (Document, Spreadsheet, Slides and so on) can be downloaded by the method of "Files: get" in Drive API.

  • When you want to download the Google Docs files, please use the method of "Files: export" as follows.

      GET https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]
    

Sample curl command:

As a test, when you use curl, the sample curl command is as follows.

curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]"
curl "https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"

References: