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