Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要求Google作为OAuth的一部分返回电子邮件地址_Oauth_Dotnetopenauth - Fatal编程技术网

要求Google作为OAuth的一部分返回电子邮件地址

要求Google作为OAuth的一部分返回电子邮件地址,oauth,dotnetopenauth,Oauth,Dotnetopenauth,我正在使用OAuth访问带有dotNetOAuth的Gmail。我如何强制谷歌在授权后返回用户的电子邮件地址作为回调的一部分 默认情况下,Google OAuth callback只返回令牌secret和access令牌。OAuth在OAuth握手过程中不提供额外参数的工具,因此我认为您不能强制Google提供它。不过,可能有一个Google API,您可以在握手后使用OAuth访问令牌调用以获取电子邮件地址。首先,您需要将以下作用域()添加到OAuth请求中 从Google返回应用程序并拥有访

我正在使用OAuth访问带有dotNetOAuth的Gmail。我如何强制谷歌在授权后返回用户的电子邮件地址作为回调的一部分


默认情况下,Google OAuth callback只返回令牌secret和access令牌。

OAuth在OAuth握手过程中不提供额外参数的工具,因此我认为您不能强制Google提供它。不过,可能有一个Google API,您可以在握手后使用OAuth访问令牌调用以获取电子邮件地址。

首先,您需要将以下作用域()添加到OAuth请求中

从Google返回应用程序并拥有访问令牌后,您可以使用访问令牌向
https://www.googleapis.com/userinfo/email?alt=json

这将返回电子邮件地址。更多信息请访问

请求OAuth范围包含“电子邮件显示范围”

然后使用RESTAPI获取地址

            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);

在php中,apiouth2service.php类提供了访问登录用户信息的方法。为此,可以使用userinfo->get()方法。确保您也使用范围

这将使用相同的访问令牌。此外,您还应该尝试在其他API中查找类似的信息。通过oAuth#u Playery>>查看这一点要容易得多。这里有一个c#函数,用于当您预先授权请求时,如上所述:

        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
是的一部分)

如果您请求userinfo.email范围,Google将返回一个id令牌和访问令牌

可以在www.googleapis.com?/oauth2/v1/tokeninfo?id_-token=IDTOKENHERE对id_令牌进行解密以提供用户的电子邮件地址


此处的更多信息:

您是否找到了可用于获取用户电子邮件地址的API调用?否。此外,由于OAuth在不同提供商上的实现不同,我们决定放弃该要求,并实施业务解决方案。它只是让我想起了一句话“关于标准的最好的事情是有很多标准”。这个链接可能会帮助那些在签署谷歌API请求时遇到困难的人(你需要添加授权标题):这对我不起作用。如果这对你来说也不合适,你还可以连接到谷歌联系人(除了连接Gmail)获取你的电子邮件地址。请参阅:在我的outh请求中添加userinfo.email作用域对我来说效果很好。之后,我可以从oauth响应中检索电子邮件。同时查看并验证您的令牌是否不起作用。。。远程服务器返回错误:(403)禁止。此URL(
)https://www.googleapis.com/userinfo/email?alt=json&access_token=“+token
)正是我所需要的。非常感谢。你好,纳伦。我没有收到回复中的电子邮件。奇怪的是,我有另一个应用程序使用OAuth,该应用程序运行完美,响应中包含电子邮件。它应该是“access\u token”,而不是“id\u token”
        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;
        }
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...