Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Userinfo from Oauth2 Google Contacts API

Error which i am getting:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}

Below code, i am using:

GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
    .setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).build();
credential.setAccessToken(tokenResponse.getAccessToken());
credential.setAccessToken(tokenResponse.getRefreshToken());

Till here, i get Refresh token, Access Token, etc

Oauth2 userInfoService = new Oauth2.Builder(this.TRANSPORT,
        this.JSON_FACTORY, credential.getRequestInitializer())
        .setApplicationName(Constants.APPLICATION_NAME).build();

It fails at below line: (Dont know, Why?)

Userinfo userInfo = userInfoService.userinfo().get().execute();

I searched on web, and i get very less examples of it and rare materials. Any body has any idea on it?

What am i doing wrong?

like image 770
Love Sharma Avatar asked Jul 22 '12 17:07

Love Sharma


People also ask

How to connect to Google API using OAuth?

To begin, obtain OAuth 2.0 client credentials from the Google API Console. Then your client application requests an access token from the Google Authorization Server, extracts a token from the response, and sends the token to the Google API that you want to access.

How do I get OAuth credentials from Google?

Obtain OAuth 2.0 credentials from the Google API Console. Visit the Google API Console to obtain OAuth 2.0 credentials such as a client ID and client secret that are known to both Google and your application. The set of values varies based on what type of application you are building.

What version of OAuth does Google use?

Note: Use of Google's implementation of OAuth 2.0 is governed by the OAuth 2.0 Policies. Google APIs use the OAuth 2.0 protocol for authentication and authorization.

How do I get a Google API client ID?

Visit the Google API Console to obtain OAuth 2.0 credentials such as a client ID and client secret that are known to both Google and your application. The set of values varies based on what type of application you are building.


1 Answers

I'm guessing credential.getRequestInitializer() is null.

I've solved this by setting an custom request initializer to the credential object like this

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
.setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).setRequestInitializer((new HttpRequestInitializer(){
                @Override
                public void initialize(HttpRequest request)
                        throws IOException {
                    request.getHeaders().put("Authorization", "Bearer " + accessToken);
                }
            })).build()

Google's documentation especifies the following:

** For example, a call to the UserInfo API using the access_token query string parameter looks like the following:

GET https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} A call to the same API using the access token in the HTTP header looks like the following:

GET /oauth2/v1/userinfo HTTP/1.1 Authorization: Bearer {accessToken} Host: googleapis.com**

Hope this will help you

like image 189
javiercbk Avatar answered Sep 28 '22 00:09

javiercbk