Require Google to return email address as part of OAuth

后端 未结 7 699
情歌与酒
情歌与酒 2020-12-23 10:23

I am using OAuth to access Gmail with dotNetOAuth. How can I force Google to return user\'s email address as part of callback after authorization?

By default, Google

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 10:37

    For getting the Email Id, you need to add the scope "https://wwww.googleapis.com/auth/userinfo.email"
    
    Then you will get id_token in the response.
    
    Response={
       "access_token" : "ya29.eAG__HY8KahJZN9VmangoliaV-Jn7hLtestkeys",
       "token_type" : "Bearer",
       "expires_in" : 3600,
       "id_token" : "id_token_from_server",
       "refresh_token" : "1/GIHTAdMo6zLVKCqNbA"
     }
    
    Then use this id_token as below POST request:
    
    https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=id_token_from_server
    
    And you will get response like below:
    
    Response={
     "issuer": "accounts.google.com",
     "issued_to": "80780.apps.googleusercontent.com",
     "audience": "8078909.apps.googleusercontent.com",
     "user_id": "1118976557884",
     "expires_in": 3598,
     "issued_at": 1456353,
     "email": "emailId@gmail.com",
     "email_verified": true
    }
    
    Make sure you add "www" in the APIs as shown above...
    

提交回复
热议问题