2013-04-22 63 views
3

我已经为gData和Drive C#APIs工作了两个Oauth2实现,分别在OAuth2Parameters和AuthorizationState中存储了令牌信息。我可以刷新令牌并将它们用于必要的API调用。我正在寻找一种方法来使用它来获取用户的信息,主要是电子邮件地址或域名。从Dotnet Google 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的版本,所以我不知道认为就是这样。

所有其他codesamples我看到使用UserInfo API的提及,但我找不到任何类型的C#/ dotnet api,我可以使用它,而不只是做直接的GET/POST请求。

有没有办法使用我已经拥有的C#API之一的令牌来获取这些信息,而无需发出新的HTTP请求?

回答

6

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

+0

谢谢!对于那些有我的问题的人来说,我仍然面临着如何制作凭证的问题,但是我可以从上面的[Retrieve](https://developers.google.com/drive/credentials? hl = en)例如:'GetAuthenticatorFromState'和'StoredStateClient'。从那里我创建了一个新的'Google.Apis.Services.BaseClientService.Initializer',将它的'Authenticator'字段设置为'GetAuthenticatorFromState(MyStoredAuthState)',然后将该初始化器传递给服务构造函数。一样容易,谢谢! – squid808 2013-04-24 21:17:59

+0

@ squid808你可以发布你所做的? – user990635 2013-11-10 13:38:37

+0

@ user990635我正在完全更新我的代码以使用新的和未折旧的[Google.Apis.Auth](http://google-api-dotnet-client.blogspot.com/2013/10/宣布 - 发布-160-beta-new.html),所以现在的事情有点乱,但如果我不记得回来给你一个链接提醒我一周左右,我是接近完成。 – squid808 2013-11-11 17:53:49

1

对于上述@ 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(); 
       }