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

本文关键字:用户 电子邮件 信息 获取 API Dotnet Google | 更新日期: 2023-09-27 18:10:19

我已经为gData和Drive c# api工作了两个单独的Oauth2实现,分别在OAuth2Parameters和AuthorizationState中存储令牌信息。我能够刷新令牌并将它们用于必要的API调用。我正在寻找一种方法来使用它来获取用户的信息,主要是电子邮件地址或域名。

我尝试遵循检索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'.

我刚获取了最新版本的Oauth2 api dll,所以我不认为这是它。

我看到的所有其他代码示例都提到使用UserInfo API,但我找不到任何类型的c#/dotnet API,我可以使用它而不简单地直接做GET/POST请求。

是否有一种方法来获得这个信息使用令牌我已经有一个c# api,而不做一个新的HTTP请求?

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

您需要使用Oauth2Service来检索用户的信息。

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

Oauth2Service在以下库中可用:https://code.google.com/p/google-api-dotnet-client/wiki/APIs#Google_OAuth2_API

对于@user990635的问题。虽然这个问题有点过时了,但下面的内容可能会对你有所帮助。代码使用google . api . 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();
                }