I'm using Google's API services for the first time for a personal Java project involving reading data from a Google Sheet and writing information relating to it to a separate Google Doc.
I've used Google's Quickstart Guides for both listed here and here. I managed to get everything up and running just fine when following the Google Sheets guide and have tested it with my own file as well.
However, attempting to set up the API for the Doc seems to give issues when trying to add in my newly generated credentials (Google Doc) to the already created credentials.json file. I assume that this is due to the fact that the credentials were already created for my Sheets file, and so I cannot simply add in the newly created credentials. I also attempted doing this by creating a new credentials file named doccredentials.json to attempt to use both, but am met with this when attempting to perform gradle run
again in order to compile the Google Doc API services:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Request had insufficient authentication scopes.",
"status" : "PERMISSION_DENIED"
}
My best guess is that the credentials for the Sheet are being used, but I'm not sure how to circumvent that. I've also ensured that my CREDENTIALS_FILE_PATH
variable is set to the right location too so I'm pretty sure that it is attempting to read from the right file.
Any suggestions would be appreciated. Thanks.
EDIT: Here is what I have in my authentication block of code. It was originally set up just to authenticate the Sheet API, however, after double checking it seems it may most likely be due to setting the Scopes incorrectly. Any suggestions on how to allow the scope to be for both Spreadsheets and Docs?:
private static Credential authorize() throws IOException, GeneralSecurityException {
InputStream in = InventoryManagerSheet.class.getResourceAsStream("/credentials.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(),
clientSecrets, scopes).setDataStoreFactory(new FileDataStoreFactory(new java.io.File("tokens"))).setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
return credential;
}