简体   繁体   中英

OAuth: Send Access Token and Get User data

I am trying Google OAuth.I am developing web application running on IBM webSphere server. Application's framework is SpringMVC.

I am trying to get userinfo.profile. I have successfully get Access Token and could not find a way to use this token and get user information.

I was redirecting browser to this URL https://www.googleapis.com/oauth2/v2/userinfo?access_token="+access.getAccessToken(); but getting error

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

Am I using this access token fine or should I send request in some other form?

I am sending request in this way:

GenericUrl shortenEndpoint = new GenericUrl("https://www.googleapis.com/oauth2/v2/userinfo");
HttpRequest request1 = rf.buildGetRequest(shortenEndpoint);
GoogleHeaders headers = new GoogleHeaders();
headers.setContentType("application/json; charset=UTF-8");
headers.setAuthorization("OAuth " + accessToken); 
request1.setHeaders(headers);
HttpResponse shortUrl = request1.execute();

After giving my Access Token in this URL in browser

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=******

I got

{
 "issued_to": "*****************",
 "audience": "*****************",
 "scope": "https://www.googleapis.com/auth/urlshortener",
 "expires_in": 3515,
 "access_type": "online"
}

Looking at the tokeninfo response, there is a mismatch between the scope for which the token was issued and the API you are trying to access. You seem to have an access token for https://www.googleapis.com/auth/urlshortener scope. If you want to user the userinfo API, you should obtain a token for the https://www.googleapis.com/auth/userinfo.profile and https://www.googleapis.com/auth/userinfo.email scopes.

It seems that your authorization header is incorrect.

Try with : headers.setAuthorization("OAuth " + accessToken);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM