We just announced a new release of the .NET client library for Google APIs as you can find in this announcement post. The blogpost contains all the release highlights, so you should take a look at it before you upgrade.
This release introduced a new Google.Apis.Auth NuGet package for making authenticated calls to Google services. It supports .NET 4 applications as well as Windows Phone, Windows Store applications and Portable Class Libraries.
For supporting ASP.NET MVC applications we also provide the Google.Apis.Auth.MVC package.
The old Google.Apis.Authentication package, which depends on DotNetOpenAuth, is now obsolete and we are not going to support this package any more.
Feel free to read more in our OAuth2 wiki page for instructions how to use this package on different Windows platforms with different OAuth2 flows.
The following lines of code present how easy it is to use a Google service from WP:
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read),
new[] { DriveService.Scope.Drive },
"user", CancellationToken.None);
var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "WP Drive Sample Application”
});
// DO YOUR MAGIC HERE… Sample code could look like the following:
var list = await service.Files.List().ExecuteAsync();And in a Windows Store applications your code should look something like the following:
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new Uri("ms-appx:///Assets/client_secrets.json"),
new[] { Uri.EscapeUriString(CalendarService.Scope.Calendar) },
"user", CancellationToken.None);
var service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Store sample",
});
// DO YOUR MAGIC HERE… Sample code could look like the following:
var calendarListResource = await service.CalendarList.List().ExecuteAsync();
It looks like it's the exact same code, but of course that's our intention - having one user experience for every platform. You can imagine what sample code for .NET 4 applications would look like.
Things you should be aware of:
- GoogleWebAuthorizationBroker is a utility class which exists in the WP and WinRT extensions. You will get it from NuGet when you target the appropriate platform. It manages all the OAuth2 "dance" for you, including redirecting the user to Google authorization server for first login and saving the user's access token and refresh token (for future use) in a specific data store for WP or WinRT applications, etc.
- UserCredential, the output of the AuthorizeAsync method, implements IConfigurableHttpClientInitializer, IHttpExecuteInterceptor and IHttpUnsuccessfulResponseHandler. It implements the initializer so it will be able to add itself as an unsuccessful response handler (for refreshing the token on 401 responses) and as an execute interceptor (to add the authorization header for every request). Read more here.
- Plugging this OAuth2 solution into your service is done by adding the UserCredential as HttpClientInitializer. That's all!
Indian Summer, Upstate NY 2013 |
Happy 1.6.0-beta!
Happy Halloween!
Enjoy,
Eyal
Thanks. So, I've gotten a working example of the Drive.v2 based on the Installed Applications example in your link, and got it working with my ClientID and Secret instead of the json file. You mention this supports .NET 4, but if I target 4 (client profile or full) I get errors on the async bits since those were introduced in 4.5. So I would update to 4.5 and just ask my users to do the same, but then I also have needs to use service accounts which says it's only available on .net 4.0. I'd prefer to keep everything in one project... is there some non-async method to create the UserCredential I'm missing? If so I could just keep using 4.0 and call it good.
ReplyDeleteI'm pretty sure that it's not recommended to change the target of your project after you already installed on its NuGet references.
DeleteYou should start a new clean project that targets .NET 4.0 (most of our samples target .NET 4.0), install the references you need and then add your code. It should work!
So here's what I know - I'm using VS 2010 at work which apparently only can use up to .NET 4.0 anyways (http://goo.gl/ljDQ1z - just found that out, good to know). So when I create a new project, install the Google Books package using NuGet (the Installed Application example in the OAuth2 page says it's calendars btw, and has a typo in 'bookselve' in the last line of text btw), and then copy the working code in I get all the warnings and errors concentrating on the Wait(), async and await lines (28, 42, 47 and 60 if you copy and paste directly).
DeleteFrom what I'm seeing on MSDN, the async programming you have in that example is new to 4.5 (http://goo.gl/giOek). I'm inferring from this that there were previously other ways of handling this in 4.0 and before that were less easy, but honestly I don't think I'm quite ready for threading - my head is spinning enough with all the new APIs and Oauth2 and all that as it is =)
We use http://www.nuget.org/packages/Microsoft.Bcl.Async/ package which supports the new await and async keywords in .NET 4.0.
DeleteAlthough I'm using VS2012, I'm pretty sure I tested it in VS2010 as well. You should even consider installing VS2013 express.
Anyway feel free to continue this discussion on StackOverflow (http://stackoverflow.com/questions/tagged/google-api-dotnet-client) which I feel is a better platform for your great feedback. THANKS!
Is there a reason for such frequent changes to authentication? It makes it hard to use the various different Google APIs together when none of them uses the same authentication scheme, to the point where I've found trying to get authentication to work the most time-consuming part of learning to use a Google API and have in a couple cases just given up and redone the APIs using the protocol docs rather than keep trying to figure out what was wrong. On top of that, the documentation is pretty spare and frequently out-of-date.
ReplyDeleteHi,
DeleteWe worked lately on targeting those exactly problems that you mentioned - the bad documentation and the awful code that you needed to copy (without realizing why...).
As I see it, after this huge change it should be much simpler and clearer to use the client library. See our OAuth2 page (https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2), I just improved that doc to be much more readable. Let me know your thoughts.