[英]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.Authentication.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.