简体   繁体   English

将authToken用于Google Health数据

[英]Using authToken for Google Health Data

We have developed and published an app for Google Health. 我们已经开发并发布了适用于Google Health的应用。 Now we want to avoid every time logging into the gmail account by asking username and password. 现在,我们希望避免每次通过询问用户名和密码来登录gmail帐户。 So as to do this i have heard that I can have following options. 为此,我听说我可以有以下选择。 1. OAuth 2. Account Manager 3. 1. OAuth 2.帐户管理器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. OAuth的问题在于它将通过Android-> Web应用程序->运行状况路径运行,因此我将需要开发一个Web应用程序,而我们现在不希望这样做。

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. 因此,我尝试使用“帐户管理器”,这是我的代码,可以用来获取帐户列表和所选帐户的有效authToken。

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. 因此,我们过去通常使用URL来获取Feed。
And now I want to skip the login process and use the above authToken for retrieving the feed. 现在,我想跳过登录过程,并使用上面的authToken检索提要。 How can this be done? 如何才能做到这一点?

Any help would be really appreciated!!! 任何帮助将非常感激!!! Thanks in advance, 提前致谢,

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. 由于标准的OAuth程序应该可以工作,因此您可以在WebView(或类似的东西)中打开带有所有必需参数的OAuth URL,用户向Google(而非您的应用)提供其用户名和密码,然后google为您提供令牌您可以将其用于将来的通信。

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. 他们只会将其提供给google,而google会为您提供令牌,该令牌将在您每次使用它时对您的应用进行身份验证。

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. 我认为您应该这样做,因为它只需要打开一个WebView一次,除非用户使用您的应用程序退出Google或拒绝访问您的应用程序。

After getting the token, you just start polling google with that token and never ask user for their login credentials. 获取令牌后,您就可以开始使用该令牌对Google进行轮询,并且永远不会要求用户提供其登录凭据。 quite seamless. 相当无缝。

UPDATE 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. 在聊天中我们聊完之后,让我告诉您,您必须在google上注册一个应用程序,这将为您提供一个appID,您的Android应用程序将使用此appID来告知Google,它代表Google要求获得许可此appID所引用的应用程序。

UPDATE 2 更新2

  • open the Google OAUth with all the parameters, google will give you a code 用所有参数打开Goog​​le OAUth,Google会给你一个代码
  • use that code and create a POST request again to google, and google will now return a long lasting AccessToken 使用该代码并再次向Google创建POST请求,Google现在将返回一个持久的AccessToken

You can then use this AccessToken in all your future communications 然后,您可以在以后的所有通信中使用此AccessToken

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM