本文介绍了Android 客户端中来自 AccountManager 的 AuthToken 不再工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很生气.我正在尝试使用 Java 中的 Google App Engine 作为服务器为 Android 构建一个回合制多人在线游戏.

I'm pretty exasperated. I'm attempting to build a turn-based multiplayer online game for Android using Google App Engine in Java as the server.

它们看起来很合适.Android 需要 Google 帐户,而 GAE 使用 Google 帐户进行身份验证,同时免费且可扩展.

They seem like a perfect fit. Android requires a Google account, and GAE uses a Google account for authentication, while being free and scalable.

因此在假期之前,我能够使用 Android 2.0 中的新 AccountManager API 从我的 Android 客户端获得对我的 GAE 应用程序的身份验证.下面的代码可以让你访问用户谷歌账户的AuthToken,然后使用它进行认证,这样用户就不用手动输入自己的账户用户名和密码了:

So before the holidays I was able to get authentication to my GAE app from my Android client using the new AccountManager API in Android 2.0. The following code allows you to access the AuthToken of the user's Google account and then use it for authentication, so that the user does not have to manually enter their account username and password:

   AccountManager mgr = AccountManager.get(this);
   Account[] accts = mgr.getAccountsByType("com.google");
   Account acct = accts[0];
   AccountManagerFuture<Bundle> accountManagerFuture = mgr.getAuthToken(acct, "ah", null, this, null, null);
   Bundle authTokenBundle = accountManagerFuture.getResult();
   String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

然后我能够将生成的 AuthToken 字符串附加到适当的 URL 并获得一个有效的 cookie,然后我可以将其用于所有进一步的请求.唯一的问题是,上周某个时候它刚刚停止为我工作.现在,当我尝试使用上述代码中的 AuthToken 时,没有返回 cookie,并且我的代码为丢失的 cookie 抛出 NullPointerException.

I was then able to append the resulting AuthToken string to the appropriate URL and get a valid cookie which I could then use for all further requests. Only thing is, sometime last week it just stopped working for me. Now when I try to use the AuthToken from the above code, I don't get a cookie returned and my code throws a NullPointerException for the missing cookie.

当我回到旧方式时,当用户手动输入他们的 Google 用户名和密码时,我从https://www.google.com/accounts/ClientLogin",效果很好.

When I go back to the old way, when the user was manually entering in their Google username and password and I get the AuthToken from "https://www.google.com/accounts/ClientLogin", it works just fine.

请告诉我,有人在用户手机上使用来自 Google 帐户的 AuthToken 为 Google App Engine 应用程序构建了一个 Android 客户端,并告诉我为什么这不再起作用的一些线索.

Please tell me someone out there has built an Android client for a Google App Engine app using the AuthToken from the Google account on the user's phone, and give me some clue as to why this is no longer working.

我真的很想完成这项工作.我的替代方案是要求用户输入他们的凭据(这很笨拙,而且他们不应该这样做),或者为服务器使用其他解决方案.

I'd really like to make this work. My alternatives are to require the user to enter their credentials (which is clunky, and which they shouldn't have to do), or going with another solution for the server.

提前致谢.

推荐答案

从 Google 工程师那里得到了这方面的帮助.原来我的 authToken 已过期.我最初是在 12 月初(确切地说是 9 日)开始实施的.显然 AccountManager 所做的是缓存 authToken,所以我从 12 月 9 日起就一直在使用相同的 authToken.当我假期回来时,它已经过期了.

Got help for this from a Google engineer. Turns out my authToken was expired. I had initially gotten the implementation working in early December (the 9th to be exact). Apparently what the AccountManager does is cache the authToken, so I had been using the same authToken since Dec. 9th. When I got back from the holidays it had expired.

为了解决这个问题,我现在调用 getAuthToken,然后对该令牌调用 invalidateAuthToken,然后再次调用 getAuthToken.这会生成一个有效的 authToken 并且工作得很好,即使它有点笨重,如果 AccountManager 每次都获得一个新的 authToken 或者检查缓存的 authToken 是否已过期,这将是不必要的.

To solve the issue, I now call getAuthToken, then call invalidateAuthToken on that token, then call getAuthToken again. This generates a valid authToken and works just fine, even if it is a little clunky and would be unnecessary if AccountManager just got a fresh authToken each time, or did a check to see if the cached one was expired.

请注意,您不能将令牌类型与帐户类型混淆:invalidateAuthToken 必须使用com.google"而不是ah"来调用,否则它会静默失败.

Note that you must not mix up token type with account type: invalidateAuthToken must be called with "com.google" instead of "ah" or it will silently fail.

这篇关于Android 客户端中来自 AccountManager 的 AuthToken 不再工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 20:28