Reputation: 1909
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.)
Upvotes: 3
Views: 3754
Reputation: 201388
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.
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]
As a test, when you use curl, the sample curl command is as follows.
Files except for Google Docs files:curl "https://www.googleapis.com/drive/v3/files/[FILEID]?alt=media&key=[YOUR_API_KEY]"
Google Docs files:
curl "https://www.googleapis.com/drive/v3/files/[FILEID]/export?mimeType=[mimeType]&key=[YOUR_API_KEY]"
Upvotes: 5