2013-04-22 67 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(); 
       }