Warren Buckley
Warren Buckley

Reputation: 1431

How do I use oAuth with Google C# .NET APIs?

I am trying to use the .NET APIs from Google https://code.google.com/p/google-api-dotnet-client/ https://www.nuget.org/packages/Google.Apis.Authentication/

As most of these APIs from Google require an oAuth token for you to fetch any data from them, I am struggling to get over this first hurdle.

My plan is to create a very simple .NET Web API that lists out my Google Analytic's accounts via the API which I can then in turn use with AngularJS to bind to a nice little view with the JSON returned from my WebAPI call.

The problem I mentioned is that there seems to be lots of different examples floating about and I can't seem to get any to to work. Either its out of date or the sample project doesn't compile etc...

From what I can understand I need to pass an Authenticator object to the BaseClient object which is in turn used for any Service API.

Below is a rough example I am trying to get to work, but currently fails and gets an error back from Google's API because I am not logged in/passing an oAuth token. Can anyone give me any pointers or advice please.

Thanks, Warren

[HttpGet]
public IEnumerable<Account> GetAccounts()
{
    var baseClient              = new BaseClientService.Initializer();
    baseClient.ApplicationName  = "Google Stats Example";
    baseClient.Authenticator    = null; //Unsure how to get the oAuth token

    AnalyticsService stats      = new AnalyticsService(baseClient);
    var accounts                = stats.Management.Accounts.List();
    var allAccounts             = accounts.Execute();

    return allAccounts.Items;
}

Upvotes: 2

Views: 5592

Answers (1)

Jipo
Jipo

Reputation: 131

To create an authenticator you need a nuget package named "Google APIs OAuth2 Client Library" but i downloaded a nugget package named "Google.Apis.Oauth2.v2 Client Library". Similar names but not the same.

I was not able to find the correct one on nugget page. If you perform a search on that package it wont show up. I had to use Visual Studio Nugget searcher and activate "show prerealese packages" to install it.

Upvotes: 1

Related Questions