【问题标题】:Get user email info from a Dotnet Google API从 Dotnet Google API 获取用户电子邮件信息
【发布时间】:2013-04-22 21:57:31
【问题描述】:

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

我尝试按照 Retrieve OAuth 2.0 Credentials 的演示进行操作,但我收到了类似于 rapsalands 的问题 here 的编译错误,说它

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

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

所有其他 code samples 我看到周围提到使用 UserInfo API,但我找不到任何类型的 C#/dotnet api 可以使用它而无需简单地执行直接 GET/POST 请求.

有没有办法使用我已经拥有的令牌和其中一个 C# api 来获取此信息,而无需发出新的 HTTP 请求?

【问题讨论】:

    标签: c# .net google-drive-api gdata


    【解决方案1】:

    您需要使用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

    【讨论】:

    • 谢谢!对于那些有我的问题的人,我仍然存在如何制作凭据的问题,但我可以从上面的Retrieve 示例中借用几个成员:GetAuthenticatorFromStateStoredStateClient。从那里我创建了一个新的Google.Apis.Services.BaseClientService.Initializer,将其Authenticator 字段设置为GetAuthenticatorFromState(MyStoredAuthState),然后将该初始化程序传递给服务构造函数。很简单,谢谢!
    • @user990635 我正在完全更新我的代码以使用新的且未折旧的Google.Apis.Auth,所以目前情况有点混乱,但如果我不记得一周左右回来给你一个链接提醒我,我快完成了。
    【解决方案2】:

    对于上述@user990635 的问题。虽然这个问题有点过时,但以下内容可能会对某人有所帮助。代码使用 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();
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2015-02-22
      相关资源
      最近更新 更多