I can't seem to be able to utilize the Google API with Oauth. What am I missing?
Error Message:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_scope",
"error_description" : "Invalid oauth scope or ID token audience provided."
}
Java code:
private void printLabels() {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
List<String> scopes = new ArrayList<>();
scopes.add(GmailScopes.GMAIL_LABELS);
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("C:\\test\\credential.json"));
credential.createScoped(scopes);
credential.refreshToken(); // error happens here
String appName = "VS";
Gmail.Builder builder = new Gmail.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(appName);
Gmail gmail = builder.build();
Object o = gmail.users().labels().list("me").execute();
System.out.println("o = " + o);
}
Google API Configuration:
- Logged in to https://console.developers.google.com/
- Created project
- Enabled Gmail API
- Created Service Account (assigned owner role)
- Download json credentials file
- Enabled OAuth consent screen - internal (not sure I need this since I only want to access my emails)
- Enabled service account domain wide delegation (not sure I need this either)