Venkat
Venkat

Reputation: 39

sheets api error access denied exception desp Requested client not authorized

Hi I am using Google apis.. I have successfully able to access google drive and google calendar but when i try to access google spreadsheet i am getting following exception.

Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 403 Forbidden { "error" : "access_denied", "error_description" : "Requested client not authorized." }

Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 403 Forbidden { "error" : "access_denied", "error_description" : "Requested client not authorized." }

My code is as follows

private static Credential authorize() throws Exception {
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

    String SERVICE_ACCOUNT_EMAIL = "[email protected]";
    List<String> SCOPES = Arrays.asList("https://spreadsheets.google.com/feeds/");

    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory).setServiceAccountId(SERVICE_ACCOUNT_EMAIL).setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(
                    new java.io.File("xxx.p12"))
            .setServiceAccountUser("[email protected]")
            .build();
    return credential;
}

public static void main(String[] args) throws Exception {

    Credential credential = authorize();
    SpreadsheetService service = new SpreadsheetService("new data service ");
    service.setProtocolVersion(SpreadsheetService.Versions.V3);
    service.setOAuth2Credentials(credential);
    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    // Make a request to the API and get all spreadsheets.
    SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
    List<SpreadsheetEntry> spreadsheets = feed.getEntries();

    // Iterate through all of the spreadsheets returned
    for (SpreadsheetEntry spreadsheet : spreadsheets) {
      // Print the title of this spreadsheet to the screen
      System.out.println(spreadsheet.getTitle().getPlainText());
    }

}

Thanks

Upvotes: 0

Views: 660

Answers (1)

Venkat
Venkat

Reputation: 39

I was only passing the spreadsheet scope, I have to pass both drive and sheet scope. Now I am able to read spreadsheets from drive.. Here is the corrected code.

    String SERVICE_ACCOUNT_EMAIL = "[email protected]";

    ArrayList<String> scopes = new ArrayList<String>();
    scopes.add(0, DriveScopes.DRIVE);
    scopes.add(1, "https://spreadsheets.google.com/feeds");


    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory).setServiceAccountId(SERVICE_ACCOUNT_EMAIL).setServiceAccountScopes(scopes)
            .setServiceAccountPrivateKeyFromP12File(
                    new java.io.File("xx.p12"))
            .setServiceAccountUser("[email protected]")

            .build();

Upvotes: 1

Related Questions