I have an application that is using a service account (because it runs from a cron job so I can't have it authorize through a user in a browser). Here's how I create the sheets service:
private static final String APPLICATION_NAME = "my app";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static HttpTransport HTTP_TRANSPORT;
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);
private static Credential authorize() throws IOException, GeneralSecurityException {
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("key.json"), HTTP_TRANSPORT, JSON_FACTORY);
credential = credential.createScoped(SCOPES);
return credential;
}
static Sheets getSheetsService() throws IOException, GeneralSecurityException {
Credential credential = authorize();
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
Every time I run it, I get a "com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden" error. Any idea on how to resolve this?