Search code examples
androidgoogle-data-api

Using authToken for Google Health Data


We have developed and published an app for Google Health. Now we want to avoid every time logging into the gmail account by asking username and password. So as to do this i have heard that I can have following options. 1. OAuth 2. Account Manager 3.

The problem with OAuth is that it will go via Android -> Web App -> Health path so i will need to develop an web app as well which we dont wish to do it right now.

So I am trying to use Account Manager, here is my code with which I could get list of accounts and an valid authToken for the selected account.

AccountManager mgr = AccountManager.get(getApplicationContext()); 
        System.out.println("Got account manager");

        Account[] accts = mgr.getAccounts();
              } 
        Account acct = accts[0];   
        AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, "android", null, this, null, null); 
        Bundle authTokenBundle = accountManagerFuture.getResult();  
        System.out.println("Account name "+accts[0].name);

        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString(); 
        System.out.println("Got token:"+authToken);

But now I am confused about how to use this token to access the health data. For getting the demographic feed we used the following code,where we explicitly made user to login into our application.

    String queryURL = "https://www.google.com/health/feeds/profile/ui/" + profileId +"/-/DEMOGRAPHICS";
getDemoGrInfoQuery = new Query(new URL(queryURL));
        Feed dempGrResultFeed;
        globals = new Globals();
    dempGrResultFeed = healthService.query(getDemoGrInfoQuery, Feed.class);

And thus we used to get the Feed using the URL.
And now I want to skip the login process and use the above authToken for retrieving the feed. How can this be done?

Any help would be really appreciated!!! Thanks in advance,


Solution

  • As the standard OAuth procedure is supposed to work, you open the OAuth URL in a WebView (or anything similar) with all the required parameters, users provide Google (not your app) with their user name and password, then google gives you a token which you can use for your future communications.

    This way the user doesn't have to give you their login credentials. They give it only to google, and google gives you a token which will authenticate your app every time you use it.

    I think you should be good to go with this, since it requires you to open a WebView only once, unless the user logs out of google using your application or denies access to your application.

    After getting the token, you just start polling google with that token and never ask user for their login credentials. quite seamless.

    UPDATE

    After our conversation in chat, let me tell you that you'll have to register an application with google, which will give you an appID, this appID will be used by your Android app to tell google that it is requesting permission on behalf of the Application which this appID refers to.

    UPDATE 2

    • open the Google OAUth with all the parameters, google will give you a code
    • use that code and create a POST request again to google, and google will now return a long lasting AccessToken

    You can then use this AccessToken in all your future communications