3

I'm currently trying to write a task manager in android which syncs with google tasks. The app uses google client API along with AccountManager to communicate with google servers. It works under android. However, I want to run it under android player on Blackberry playbook. Although the .apk file converts to a blackberry application, it seems that AccountManager does not work under playbook android player as it is not tied to a google account. I'm finding it difficult to communicate with the google servers without the account manager. I've tried adding an account manually to the AccountManager but it also throws a security exception. I'm curious if there is any other way to log into google services given an username and password (along with the API key for access)? Thanks

user1135552
  • 73
  • 1
  • 5

1 Answers1

1

The AccountManager and the Google Play Services that both allow you to go through an OAuth 2.0 authorization flow with a native experience on Android (for Google APIs only) are only available on Google Experience devices. The Android Emulator of the Blackberry Playbook is likely not a Google Experience environment.

So in that case the best way is to implement an OAuth 2.0 flow by using a WebView. This is also the technique you need to use for non-Google APIs (Facebook, Microsoft, Salesforce, Dailymotion, ...)

Basically you will have to send your new users to a special URL in a WebView where Google (or the other OAuth 2 provider) will ask them to grand you access to the APIs requested. Then you will need to extract the auth code from the URL or from the content of the page once it has been generated and returned by Google auth servers. The last step is to exchange that auth code for a refresh and an access token.

You need to read and understand how OAuth 2.0 authorization flow works for Installed application: https://developers.google.com/accounts/docs/OAuth2#installed

The step by step process to do OAuth 2.0 with a WebView on Android is as follow:

  • Redirect Users to the grant screen URL in an embeded WebView
  • Use http://localhost as the redirect URI
  • Register a WebViewClient with an onPageStarted method to intercept page changes
  • Detect successful/failed authorization by detecting redirects to http://localhost and read the auth code from the URL of the WebView
  • Finish the OAuth 2 flow by exchanging the auth code for tokens and save these tokens in local database for further use

You can find an open-source sample that does this on Onavo's GitHub.

Nicolas Garnier
  • 12,134
  • 2
  • 42
  • 39
  • The only drawback is that Google does not prefer this solution and, AFAIK, you cannot use a token obtained in this way in Google APIs Client library. Maybe I am wrong, any hints? – GoranK Nov 19 '12 at 09:37
  • Yes you are wrong :) This works perfectly. Also the fact that the Android team does not prefer this solution is not relevant because it's the only supported solution on non-Google experience devices. This has been used on iOS for instance, even the Google Client library for iOS has support for it. Updating my answer with more details and example. – Nicolas Garnier Nov 19 '12 at 10:58