0
votes

I am trying to access to google docs using OAuth2.0. I have got the client ID and secret key from Google API console. But When I am running this code, I am getting the exception. can you anyone suggest me if I am missing anything..

String CONSUMER_KEY = ".....apps.googleusercontent.com";
        String CONSUMER_SECRET = "sM52Mts_d7snVIomnJaQkjkA";

        GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
        oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
        oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);

        DocsService client = new DocsService("testing");
        client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());

        // Retrieve user's list of Google Docs
        String user = "[email protected]";
        URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full" +
                              "?xoauth_requestor_id=" + user);

        DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
        for (DocumentListEntry entry : resultFeed.getEntries()) {
          System.out.println(entry.getTitle().getPlainText());
        }

Exception:

com.google.gdata.util.AuthenticationException: Token invalid - Invalid token: Invalid user for the two legged OAuth

Token invalid - Invalid token: Invalid user for the two legged OAuth

Token invalid - Invalid token: Invalid user for the two legged OAuth

Error 401

2

2 Answers

1
votes

The xoauth_requestor_id parameter is only for 2-legged Oauth (2LO) which is used to authenticate Google Apps users. I don't think it is possible to authenticate regular gmail users using 2LO.

Have you seen the example for 3-legged Oauth here http://code.google.com/apis/gdata/docs/auth/oauth.html#Examples

1
votes

It looks like you're using a Client ID that isn't a service account. Two legged auth is only supported on service accounts. If you create a new service account and use its client ID and secret (an RSA private key), your code should work.