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?