简体   繁体   中英

Is there some way to include multiple Google API services (e.g. Sheets and Docs) for a single Java project?

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;
}

Answer related to your error message

I am writing this without seeing your code, I will edit it once you have but this might help.

"Request had insufficient authentication scopes.",

Means that the user that is currently authenticated authentication has not granted you enough permissions to do what you are trying to do.

The tutorial you mention you are following request the the following scopes from the user

private static final List<String> SCOPES = Collections.singletonList(DocsScopes.DOCUMENTS_READONLY);

You mention that you would like to write to the google doc. As you can see this scope only allows for READONLY access to the doc. YOu should probably be using but this is just a guess your going to have to check the name my be different in java client library.

private static final List<String> SCOPES = Collections.singletonList(DocsScopes.DOCUMENTS);

it could also be yes you can mix scopes.

private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

Once you have done that you need to go to CREDENTIALS_FILE_PATH and delete the credentials file that was created for the "user" this will foruce your code to request user authorization again and request write access to your file.

Answer related to your actual question

Now back to your question which has nothing to do with your error message. You can access two different apis from the scame application but you need to create a service for each

The following service would give you access to the Google docs api

Docs service = new Docs.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

The following service would give your code access to the Google drive api

Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();

Just give them different names. Remember to add scopes needed foreach api you can mix them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM