Require Google to return email address as part of OAuth

后端 未结 7 697
情歌与酒
情歌与酒 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:30

    First you need to add the following scope (https://www.googleapis.com/auth/userinfo.email) to your oauth request.

    After you're back to your app from Google and you have your access token, you can make a request using the access token to https://www.googleapis.com/userinfo/email?alt=json. This will return the email address. More info at http://sites.google.com/site/oauthgoog/Home/emaildisplayscope

    0 讨论(0)
  • 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...
    
    0 讨论(0)
  • 2020-12-23 10:37

    Here's a c# function for when you have pre-authorized the request as detailed above:

            private void FetchUsersEmail(token)
            {
                var emailRequest = @"https://www.googleapis.com/userinfo/email?alt=json&access_token=" + token;
                // Create a request for the URL.        
                var request = WebRequest.Create(emailRequest);
                // Get the response.
                var response = (HttpWebResponse) request.GetResponse();
                // Get the stream containing content returned by the server.
                var dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                var reader = new StreamReader(dataStream);
                // Read the content. 
                var jsonString = reader.ReadToEnd();
                // Cleanup the streams and the response.
                reader.Close();
                dataStream.Close();
                response.Close();
    
                dynamic json = JValue.Parse(jsonString);
                var currentGoogleEmail = json.data.email;
            }
    

    (JValue is part of JSON.Net)

    0 讨论(0)
  • 2020-12-23 10:46

    If you request the userinfo.email scope, Google returns an id_token along with the access_token.

    The id_token can be unencrypted to provide the user's email address, at www.googleapis.com?/oauth2/v1/tokeninfo?id_token=IDTOKENHERE

    More information here: https://developers.google.com/accounts/docs/OAuth2Login

    0 讨论(0)
  • 2020-12-23 10:48

    request OAuth scope to include the "Email Display Scope" https://www.googleapis.com/auth/userinfo.email

    scope="http://www.google.com/m8/feeds/ https://www.googleapis.com/auth/userinfo.email"
    

    Then use REST API like Hammock to get address

                RestClient client = new RestClient
                {
                    Authority = "https://www.googleapis.com",
                };
    
                RestRequest request = new RestRequest
                {
                    Path = "userinfo/email?alt=json",
                    Credentials = OAuthCredentials.ForProtectedResource(
                         this.requestSettings.ConsumerKey,
                         this.requestSettings.ConsumerSecret,
                         this.requestSettings.Token,
                         this.requestSettings.TokenSecret)
                };
    
                var response = client.Request(request);
    
    0 讨论(0)
  • 2020-12-23 10:51

    OAuth doesn't provide a facility for extra parameters during an OAuth handshake, so I don't think you can force Google to supply it. There is likely a Google API however that you can use your OAuth access token to call to fetch the email address after the handshake, however.

    0 讨论(0)
提交回复
热议问题