403 Error when making Google Drive API call (in Java)

I calls Google APIs via a service account.  Here is the use case.: 1. Create a Google Sheet via Google Sheet API; 2. Move the sheet to a shared folder on Google Drive.

I am able to create a Google Sheet via API and also upload a file to the shared folder.  But, when trying to move the newly created Google sheet to the shared folder, it gives me the 403 error.  I will put the codes and the error below.

Please let me know if you have any suggestions. Thanks.

Java Codes:


// Retrieve the file ID of the newly created Google Sheet
String sheetFileId = spreadsheet.getSpreadsheetId();

// Add the Google Sheet to the specified folder
File file1 = new File();
file1.setParents(Collections.singletonList("1GMej6gDQWxNw1PuCsXFUlJj1OwJTe9nu"));
serviceDrive.files().update(sheetFileId, file1).execute();

Error Message:

403 Forbidden
POST https://www.googleapis.com/drive/v3/files/1BM5fGGADc-7t2lpIn6ioBG6YnA38_RYXoaTPYSZ-76A
{
"code": 403,
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"
}
],
"errors": [
{
"domain": "global",
"message": "Insufficient Permission",
"reason": "insufficientPermissions"
}
],
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}

0 1 774
1 REPLY 1

Hi there,

This looks like you need to update the authentication scopes when you are creating your credentials to use.

Do you have a variable `scopes` that is set to the necessary scopes indicated in the API documentation?

Usually to use the Drive API you have to have a line like this (the second element of the array being the one you need):

`