简体   繁体   English

从Dotnet Google API获取用户电子邮件信息

[英]Get user email info from a Dotnet Google API

I'm have working two separate implementations of Oauth2 for both the gData and the Drive C# APIs, storing token information in an OAuth2Parameters and AuthorizationState respectively. 我已经为gData和Drive C#API两个单独的Oauth2实现,分别在OAuth2Parameters和AuthorizationState中存储令牌信息。 I'm able to refresh the token and use them for the necessary API calls. 我能够刷新令牌并将它们用于必要的API调用。 I'm looking for a way to use this to get the user's information, mainly the email address or domain. 我正在寻找一种方法来使用它来获取用户的信息,主要是电子邮件地址或域。

I tried following the demo for Retrieve OAuth 2.0 Credentials but I'm getting a compile error similar to rapsalands' issue here , saying it 我尝试了演示了以下检索OAuth 2.0凭证 ,但我发现类似rapsalands'的问题编译错误在这里 ,说它

can't convert from
'Google.Apis.Authentication.OAuth2.OAuth2Authenticator<
Google.Apis.Authenticatio‌​n.OAuth2.DotNetOpenAuth.NativeApplicationClient>'
to 'Google.Apis.Services.BaseClientService.Initializer'.

I just grabbed the most recent version of the Oauth2 api dlls so I don't think that's it. 我刚刚抓住了最新版本的Oauth2 api dll,所以我不认为就是这样。

All the other code samples I'm seeing around mention using the UserInfo API, but I can't find any kind of C#/dotnet api that I can use with it without simply doing straight GET/POST requests. 我正在使用UserInfo API提到的所有其他代码 示例 ,但我找不到任何类型的C#/ dotnet api,我可以使用它而不需要直接执行GET / POST请求。

Is there a way to get this info using the tokens I already have with one of the C# apis without making a new HTTP request? 有没有办法使用我已经拥有的一个C#apis的令牌获取此信息而不会发出新的HTTP请求?

You need to use Oauth2Service to retrieve information about the user. 您需要使用Oauth2Service来检索有关用户的信息。

Oauth2Service userInfoService = new Oauth2Service(credentials);
Userinfo userInfo = userInfoService.Userinfo.Get().Fetch();

Oauth2Service is available on the following library: https://code.google.com/p/google-api-dotnet-client/wiki/APIs#Google_OAuth2_API Oauth2Service可在以下库中找到: https//code.google.com/p/google-api-dotnet-client/wiki/APIs#Google_OAuth2_API

For @user990635's question above. 对于@ user990635上面的问题。 Though the question is a little dated, the following may help someone. 虽然问题有点过时,但以下内容可能对某人有所帮助。 The code uses Google.Apis.Auth.OAuth2 version 该代码使用Google.Apis.Auth.OAuth2版本

  var credentials =
                    await GoogleWebAuthorizationBroker.AuthorizeAsync(
                            new ClientSecrets {ClientId = clientID, ClientSecret = clientSecret},
                            new[] {"openid", "email"}, "user", CancellationToken.None);
                if (credentials != null)
                {
                    var oauthSerivce =
                        new Oauth2Service(new BaseClientService.Initializer {HttpClientInitializer = credentials});
                    UserInfo = await oauthSerivce.Userinfo.Get().ExecuteAsync();
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌获取用户的 email 地址 Oauth api - How to get user's email address using google Oauth api 如何在c#中从Google API获取个人资料信息 - How to get Profile Info from the Google API in c# Microsoft WEB API从Microsoft帐户获取用户信息 - Microsoft WEB API Get User Info From Microsoft Account 使用Ubuntu上的dotnet core从目录路径获取驱动器信息 - Get Drive info from directory path using dotnet core on ubuntu 从.NET Backend Azure Mobile Service中的身份验证令牌获取名称,电子邮件ID等用户信息 - Get user info like name, email Id etc from authentication token in .NET Backend Azure Mobile Service google + api中缺少name,given_name和family_name json属性,获取用户信息 - name, given_name and family_name json property are missing in google + api get user info 使用Google OAuth2获取用户信息,例如名称,电子邮件 - Fetching User Info like Name, Email using Google OAuth2 如何从Facebook Graph Api获取用户电子邮件? - How to get a user email from Facebook Graph Api? Google Adwords API:获取有关APIError的标准信息 - Google Adwords API: Get criterion info on APIError Twitter API,获取当前经过身份验证的用户信息 - Twitter api, get current authenticated user info
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM