Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using google client API in android without account manager

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

like image 461
user1135552 Avatar asked Sep 15 '26 02:09

user1135552


1 Answers

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.

like image 186
Nicolas Garnier Avatar answered Sep 17 '26 16:09

Nicolas Garnier