简体   繁体   中英

Google oauth2 endpoint does not return user profile info (name etc)

I'm trying to use google oauth2 on my web service (golang), but can't get user profile info (given-name, family-name).

I'm trying various endpoints but every time get this kind of answer:

{
    "id":"*************",
    "email":"***@gmail.com",
    "verified_email":true,
    "picture":"https://***/photo.jpg"
}

In the same time google playground with this set of scopes returns this kind of answer:

{
  "family_name": "***", 
  "name": "****", 
  "picture": "https://*******/photo.jpg", 
  "locale": "ru", 
  "email": "****@gmail.com", 
  "given_name": "*****", 
  "id": "************", 
  "verified_email": true
}

How can i get the same answer in my web-app? People on this site and in the internet advise to add profile scope, but I'm already done this.

Here is my code (shortened)

"golang.org/x/oauth2"
"golang.org/x/oauth2/google"

func init() {
    googleOauthConfig = &oauth2.Config{
        RedirectURL:  "*******",
        ClientID:     "*********",
        ClientSecret: "******",
        Scopes: []string{
            "https://www.googleapis.com/auth/userinfo.email",
            "https://www.googleapis.com/auth/userinfo.profile",
            "openid"},
        Endpoint: google.Endpoint,
    }
}

var googleOauthConfig *oauth2.Config

//const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v1/userinfo"
const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v2/userinfo"
//const oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v3/userinfo"
const oauthGoogleURLAPI2 = "https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses"
//const oauthGoogleURLAPI = "https://openidconnect.googleapis.com/v1/userinfo"

func HandleGoogleCallback(w http.ResponseWriter, r *http.Request) {
    ...

    oauthState, _ := r.Cookie(oauthstateCookieName)

    code := r.FormValue("code") 

    gtoken, err := googleOauthConfig.Exchange(oauth2.NoContext, code)

    bearer := "Bearer " + gtoken.AccessToken

    req, err := http.NewRequest("GET", oauthGoogleURLAPI, nil)
    req.Header.Add("Authorization", bearer)

    gresponse, err := http.DefaultClient.Do(req)

    contents, err := ioutil.ReadAll(gresponse.Body)

    authContent := &AuthContent{}

    err = json.Unmarshal(contents, authContent)

    log.Println(authContent)

    ...
}

I'm also read this article Google Oauth2 userinfo API not returning user's name data But result is the same : there is no profile info.

Thanks.

Update: On the advice of the comment I slightly corrected code of "golang.org/x/oauth2" and get id_token. When I checked this id token on jwt.io, I saw all the data I need. But I thought profile info should be retrieved through API request like this

req, err := http.NewRequest("GET", <endPoint>, nil)
gresponse, err := http.DefaultClient.Do(req)

In this example only standard go library is used and gresponse contains whole http response, isn't it? Why response does not contains fields that I need?

  1. Handle error in googleOauthConfig.Exchange(oauth2.NoContext, code)
  2. Handle error in http.DefaultClient.Do
  3. Handle error in ioutil.ReadAll(gresponse.Body)
  4. Handle error in json.Unmarshal

And you may find the answer.

Also I remember I was always sending access_token as a query param:

response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)

I found the answer. That was my fault. The structure that receive result was looking like this

type AuthContent struct {
    ID            string `json:"id"`
    Email         string `json:"email"`
    VerifiedEmail bool   `json:"verified_email"`
    Picture       string `json:"picture"`
}

This is why there is no fields like name in response.

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