4

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. I'm able to refresh the token and use them for the necessary API calls. 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

can't convert from
'Google.Apis.Authentication.OAuth2.OAuth2Authenticator<
Google.Apis.Authenticatio‌​n.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.

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.

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?

Community
  • 1
  • 1
squid808
  • 1,430
  • 2
  • 14
  • 31

2 Answers2

8

You need to use Oauth2Service to retrieve information about the user.

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

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
  • Thanks! For those with my issue, there still remained the issue for me of how to make the credentials, but I was able to borrow a couple members from the above [Retrieve](https://developers.google.com/drive/credentials?hl=en) example: `GetAuthenticatorFromState` and `StoredStateClient`. From there I created a new `Google.Apis.Services.BaseClientService.Initializer`, set its `Authenticator` field to `GetAuthenticatorFromState(MyStoredAuthState)` and then pass that intializer to the service constructor. Easy as pie, thanks! – squid808 Apr 24 '13 at 21:17
  • @user990635 I'm in the process of completely updating my code to use the new and not-depreciated [Google.Apis.Auth](http://google-api-dotnet-client.blogspot.com/2013/10/announcing-release-of-160-beta-new.html) so things are a bit messy at the moment, but if I don't remember to come back and give you a link remind me in a week or so, I'm nearing completion. – squid808 Nov 11 '13 at 17:53
2

For @user990635's question above. Though the question is a little dated, the following may help someone. The code uses Google.Apis.Auth.OAuth2 version

  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();
                }
shr
  • 875
  • 10
  • 20