• Home
  • BG topo map 1933
    • Topo map Rila Mountain
    • Topo map Vitosha Mountain
  • Forum
  • Geocoding Tool
    • Geocoding
  • Shop
  • Trips
  • About
    • Advertise
Mapsys.info
Mapping and geospatial news.

Google Tasks API and OAuth 2.0

Since we launched the Google Tasks API we have received a warm welcome from the developer community on the public forum, and a lot of question on how to make it work with OAuth 2.0 on Android. Let’s see how this can be done.

Android has its own native authorization flow and its own way of handling Google accounts as you can register them on your device. Since Android 2.0, the AccountManager manages the accounts that you have registered – the ones that are listed under Settings > Accounts & sync. Specifically, it handles the authorization flow and can generate authorization tokens that are required to access data using APIs.

Accounts registered in your Android environment

You can use the AccountManager to get the Google account you want to access the Tasks for. Though as the AccountManager also manages accounts from other vendors you can simply instantiate a GoogleAccountManager which is provided in the Google APIs Client library for Java and only handles Google accounts:

GoogleAccountManager googleAccountManager = new GoogleAccountManager(

  activity);

Account[] accounts = accountManager.getAccounts();

If more than one Google accounts are available on the Android device you should prompt the user for the account he wishes to use through a dialog.

Now that the user has chosen an account (or if there was only 1 Google account) you can ask the AccountManager to issue an authorization token for the Tasks API. This is done by calling the AccountManager.getAuthToken() method. The AccountManager will take care of contacting the Google APIs authorization endpoint and run the AccountManagerCallback that you have defined in the method call when it has retrieved an authorization token:

googleAccountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE, null,

  activity, new AccountManagerCallback() {

    public void run(AccountManagerFuture future) {

    try {

      // If the user has authorized your application to use the tasks API

      // a token is available.

      String token = future.getResult().getString(

        AccountManager.KEY_AUTHTOKEN);

      // Now you can use the Tasks API...

      useTasksAPI(token);

    } catch (OperationCanceledException e) {

      // TODO: The user has denied you access to the API, you

      // should handle that

    } catch (Exception e) {

      handleException(e);

    }

  }

}, null);

The Android AccountManager has support for OAuth 2.0. A user friendly AUTH_TOKEN_TYPE exists for the Tasks API which will make the AccountManager return an OAuth 2.0 access token:

String AUTH_TOKEN_TYPE = ”Manage your tasks”;

During the AccountManager.getAuthToken() call the AccountManager will check if your application has been authorized to access the Tasks API. If your application has not yet been authorized the user will be presented with an authorization dialog so that they can Allow or Deny your application to use the Tasks API on their account.

Authorization dialog

Another piece of getting the Tasks API to work using OAuth 2.0 on Android is that you also need to specify an API Key when making calls to the Tasks API. The API Key is mandatory as it identifies your application and therefore allows the API to deduct quota and use the quota rules defined for your project. You can get it from the Google APIs Console at API Access > Simple API Access > API Key. You need to specify the API Key on your Tasks service Object:

useTasksAPI(String accessToken) {

  // Setting up the Tasks API Service

  HttpTransport transport = AndroidHttp.newCompatibleTransport();

  AccessProtectedResource accessProtectedResource = 

    new GoogleAccessProtectedResource(accessToken);

  Tasks service = new Tasks(transport, accessProtectedResource, 

    new JacksonFactory());

  service.setKey(INSERT_YOUR_API_KEY);

  service.setApplicationName("Google-TasksSample/1.0");


  // TODO: now use the service to query the Tasks API

}

At this point you should have a fully setup Tasks API service Object which you can use to query the API as per the Tasks API developer’s Guide.

If you would like to get more tips and learn more about getting the Google Tasks API and OAuth 2.0 working on Android please have a look at our newly published article.

Also we recently added a new sample to the Google APIs Client Library for Java sample repository to help you getting started with the Tasks API and OAuth 2.0 on Android.

13/09/2011 | Posted in: Access, Android, app, device, Google, IT, map, maps, news, Twitter, Work | Tags: account, api, app, application, client, google-calendar, google-contacts, google-tasks, map, maps, news, nicolas-garnier, tasks | No Comments »
  • AXESSIVE.COM – Online shop for axes!


  • Shoping list

    Your list is empty
  • Last Entries

    • My new power hammer!!!
    • Tsar Kaloyan – Rex Blachorum et Bulgarorum
    • Веселина Вл. Георгиева – баронеса Лина Ротшилд
    • Europe Beer map
    • savethepostoffice.com
    • Политика в България
    • Top 100 Green Blogs To Follow In 2013 [Infographic]
    • Australia Added New Colors to the Weather Map
    • Happy New Year!
    • Bomb Sight interactive map – London Blitz
  • Social Networks

    Google+
     
    Linked In
     
    YouTube
     
    Facebook
     
    Twitter

  • Count & Feeds

    Enter your email address:

    Delivered by FeedBurner




    Free Page Rank Tool

  • Interesting and useful

    • AXESSIVE.COM
    • Extreme Java GPS Aplication
    • Funny Photos
    • Geospatial Blog
    • GISLounge
    • Great Google Maps tutorial site
    • Mapping and GIS news
    • Mappoint Forum
    • The Art Of Maps
  • News by Date

    June 2023
    M T W T F S S
    « Jan    
     1234
    567891011
    12131415161718
    19202122232425
    2627282930  

© 2011 mapsys.info . All information under Creative Commons License.Powered by WordPress.Edited and modified By Ivan Tasev