Wednesday, November 19, 2014

Universal Apps using the .NET client library for Google APIs

Hi all,

In the last couple weeks, Jesse and I improved the samples repository of the Google APIs client library for .NET with new samples for Windows and Windows Phone.
Jesse authored Translate.WP.TranslateText - a Windows Phone sample for the Google Translate API.
A week ago, I submitted a Blogger.Sample for Universal Store apps using the Google Blogger API.
These are the first samples in our repository that target Windows Phone 8.1 and Windows 8.1 apps using the .NET client library for Google APIs.

In this blog post I'll focus on the Universal Windows sample I created. The sample shares code between the Windows Store and Windows Phone versions, and uses MVVM best practices.

I must admit, the last time I wrote MVVM was back in 2011, back then I worked on a huge WPF project and while working on MVVM again I saw that nothing really has changed since then.

For those of you who don’t know, Universal Windows apps help you develop and share code between all Windows devices. You can read more about it in building-universal-windows-apps.
It's important to mention that I already had a hacky sample for Windows Phone 8.0 that I used to test a new release of the client library with it. I just thought that it is going to be easy to upgrade it to Windows Phone 8.1.

I WAS WRONG

I didn't find a right meme... any suggestion will be appreciated :)


Microsoft introduced significant breaking API changes when upgrading from 8.0 to 8.1. The WebAuthenticationBroker.AuthenticateAsync which I used in Windows Phone 8.0 isn't supported anymore. Starting with Windows 8.1, Microsoft supports several AndContinue methods (one of them is WebAuthenticationBroker.AuthenticateAndContinue). When you call an AndContinue method, the app is deactivated until the operation completes. You can read more in How to continue your Windows Phone Store app after calling an AndContinue method.

So, in order to support the OAuth 2.0 protocol for Windows Phone Store apps, I had to do the following:


Step 1

Create core classes that I’m going to move shortly to Google.Apis and Google.Apis.Auth new projects that will target Windows Phone 8.1. Those classes include the following:

  1. PasswordVaultDataStore
A Windows Phone implementation of IDataStore. It uses the platform PasswordVault. PasswordVault represents a credential locker and its content is private for each app and can’t be used by other apps or services in the phone.

  1. AuthorizationCodeBroker
An implementation of ICodeReceiver. The important method is ReceiveCodeAsync which should be called twice.
In the 1st time, the method calls WebAuthenticationBroker.AuthenticateAndContinue so that users would be able to authorize this app to access their personal resources.
In the 2nd time, it should be called after the app had already received the authorization code. The implementation counts on the developer to store the code response in the PasswordVaultDataStore using WebAuthResult.Name. (see changes in MainPage.xaml.cs later on…)

  1. AuthorizationCodeWPInstalledApp
An implementation of IAuthorizationCodeInstalledApp, that uses the standard AuthorizationCodeInstalledApp with the AuthorizationCodeBroker mentioned above.

  1. GoogleWebAuthorizationBroker
A helper class that puts everything together - the installed app and the password vault data store.


* All those classes are very similar to classes that we already have for supporting Windows Store applications or Windows Phone 8.0.


Step 2

In addition, each Windows Phone app that wants to support continuation should implement the following:

ContinuationManager

A custom and a very short implementation that targets authentication ONLY. The full implementation is available here: http://msdn.microsoft.com/en-us/library/dn631755.aspx. Take a look in this short version:
App.xaml.cs
The application starting point. The important method is OnActivated, which might be called after the authorization code was received. The implementation looks like the following:
MainPage.xaml.cs
The first time the users log in to the app, the app should be suspended and the users must authorizes the app in order to access their private resources. When the app is activated again, it calls the continuation manager which in turn calls the ContinueWebAuthentication method. I think that the method is self documented, so without any words, take a look:

The only piece that is still missing to you now, is the BlogsViewModel and the BloggerRepository. Those two classes (and several others) are shared between the Windows and Windows Phone projects.
BlogsViewModel is just the ViewModel of the MainPage. When the users request to get all their blogs, the GetBlogsCommand is called. Then GetBlogsAsync will be executed and in turn it will call the repository to retrieve all blogs.
And the BloggerRepository looks like the following:
* Note that the implementation of GoogleWebAuthorizationBroker is different for Windows and Windows Phone apps.
Take a look in the code sample at Blogger.Sample. You will find all the missing pieces there, like IBloggerRepository, BlogViewModel, PostViewModel, MainPage and other classes. Just remember to follow the README file in order to setup and run the app.


Upstate NY, Oct 2014


Stay tuned for further documentation - the Windows Phone 8.1 section is going to be refreshed soon.



We hope you find our new sample useful when writing Windows Phone 8.1 apps with Google Apis. Let us know if you have any questions in the comments of this post, on the client issue tracker or in Stack Overflow using the google-api-dotnet-client tag (Preferred).



Enjoy!
Eyal (upside down)

5 comments:

  1. Hi there Eyal. I need to know if Gmail Raw format can be easily migrated to outlook programmatically. I see Gmail doesn't support .eml format but I can extract the whole email in raw text using google Api. Any information regarding this issue will be very helpful to me.

    best regards,

    ReplyDelete
    Replies
    1. Hi there..
      As I'm not familiar with the Gmail API, I recommend you creating a new thread using the gmail-api tag in SO - https://stackoverflow.com/questions/tagged/gmail-api.

      Consider also taking a look in previous questions, before you create a new issue, because someone might already asked that one.
      Sorry..

      Delete
    2. No problem. at least you can tell me about a good free Mime parser library for C# which can parse RFC2822 compliant messages. Java(java mail) has built-in support for such stuff but I don't see such library in C#. I have to parse RFC2822 message and send it to Exchange server. I need your advice on this.

      Delete
    3. Sorry.... I'm not familiar with that too... I feel I'm not so helpful to you :(

      I just tried google it, and that's what I found:
      http://stackoverflow.com/questions/1669797/are-there-net-framework-methods-to-parse-an-email-mime
      http://msdn.microsoft.com/en-us/library/microsoft.exchange.data.mime.mimereader%28v=exchg.80%29.aspx
      https://github.com/jstedfast/MimeKit

      SORRY

      Delete
    4. It's ok. Your client library is helping me anyway :)

      Delete