1
votes

I'm trying to create an Android app that creates a spreadsheet on google drive and then saves data to it. The drive part works fine but for some reason when I try to use the authentication I got for google drive, and presumably the spreadsheet api(Scope https://spreadsheets.google.com/feeds/" with the spreadsheet api I get "com.google.gdata.util.AuthenticationException: Token invalid"

Here is my relevant code

    private static final String[] SCOPES = { DriveScopes.DRIVE, "https://spreadsheets.google.com/feeds/" };

    credential = GoogleAccountCredential.usingOAuth2(
            getApplicationContext(), Arrays.asList(SCOPES))
            .setBackOff(new ExponentialBackOff())
            .setSelectedAccountName(googleUserName);

    driveService = new com.google.api.services.drive.Drive.Builder(
            transport, jsonFactory, credential)
            .setApplicationName(getString(R.string.app_name))
            .build();

    //Calls to driveService work as expected
    //But this returns the invalid token exception
    SpreadSheetService service = new SpreadsheetService("AppSheetservice");

    final URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

    service.setUserToken(saleActivity.credential.getToken());
    //This line throws the invalid token error
    feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
1

1 Answers

1
votes

I finally figured out the issue is you need to call

service.setAuthSubToken(credential.getToken());

instead of

service.setUserToken(credential.getToken());