Google Endpoints – Android GoogleAuthIOException Tic Tac Toe – 删除了clientIds

我下载了Google Endpoints Tic Tac Toe示例 – Java中的服务器代码。

为了快速运行它,我从API定义中删除了clientIds,因此我可以很快看到它在API Explorer中运行:

@Api(name = "tictactoe", version = "v1") public class ScoresV1 { ... 

我可以使用API​​资源管理器运行以下方法,打开OAuth并使用我的gmail accoumnt进行身份validation。 我可以在调试器中看到“user”对象设置为该帐户:

 @ApiMethod(name = "scores.list") @SuppressWarnings("unchecked") public List list(@Nullable @Named("limit") String limit, @Nullable @Named("order") String order, User user) throws OAuthRequestException, IOException { ... 

然后我继续生成Android客户端(仍然删除了clientIds)。 我还下载了官方Android应用程序来获取UI类和res文件(帐户选择器和游戏界面)。

这就是我被困住的地方。 在真正的Android设备上运行时,TictactoeActivity中的以下代码抛出了GoogleAuthIOException:

  @Override protected ScoreCollection doInBackground(Void... unused) { ScoreCollection scores = null; try { scores = service.scores().list().execute(); } catch (IOException e) { e.printStackTrace(); 

这是例外:

 02-21 16:38:47.051: D/TicTacToe(6151): com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException 02-21 16:38:47.051: D/TicTacToe(6151): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:286) 02-21 16:38:47.051: D/TicTacToe(6151): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858) ... 

进一步深入到源代码中,我发现这个代码在GoogleAuthUtil.getToken()中抛出了根exception,这有点无益:

 com.google.android.gms.auth.GoogleAuthException: Unknown 

现在,我在ScoreV1.list()的本地开发服务器上有一个断点,在使用API​​ Explorer进行测试时遇到了这个断点,但Android客户端在没有命中服务器的情况下失败了。 现在,在本地开发服务器上设置了“-a 0.0.0.0”以接受传入连接。 我已经检查过url是否正确,我可以通过手机的Chrome浏览器运行以下url(因为没有设置用户,它给了我一个Auth错误,但没关系):

 http://192.168.2.23:8888/_ah/api/tictactoe/v1/score?limit=1&order=1 

所以我知道网络正在运行,手机可以访问它。 我还validation了AndroidManifest.xml具有以下权限集:

     

这是在TicTacToe.java中显示我的URL和路径的代码:

  public static final String DEFAULT_ROOT_URL = "http://192.168.2.23:8888/_ah/api/"; /** * The default encoded service path of the service. This is determined when * the library is generated and normally should not be changed. * * @since 1.7 */ public static final String DEFAULT_SERVICE_PATH = "tictactoe/v1/"; /** * The default encoded base URL of the service. This is determined when the * library is generated and normally should not be changed. */ public static final String DEFAULT_BASE_URL = DEFAULT_ROOT_URL + DEFAULT_SERVICE_PATH; ... 

现在,让我感到困惑的是,即使我将DEFAULT_ROOT_URL更改为随机的内容,例如5.5.5.5,我也会得到完全相同的GoogleAuthIOException! 当主机无法访问时,我希望得到一个不同的例外…

此外,我尝试将服务器部署到生产环境,并将URL重新指向[myapp] .appspot.com地址 – 结果相同。 服务器日志从未注册过请求。

事实上,我现在真的被卡住了。 请帮忙! 太感谢了!!!

该错误很可能是由于您的Android应用程序和Cloud Endpoints API之间未发生授权所致。

由于我无法完全访问您的实际Endpoint API注释和Android代码,因此我建议您仔细查看以下几点:

  • 确保您已使用clientIdsaudiences正确地注释了Cloud Endpoints API。 Android Audience值与Web Client Id的值相同。

  • 确保已部署更新的API并重新生成Endpoints API源以包含在Android应用程序中。

  • 最后,在您的Androids源代码中,您必须使用GoogleAccountCredential.usingAudience(…)构建GoogleAccountCredential的实例,然后在调用端点API时使用此凭据对象。 例如,Endpoint构建器采用H​​TTP Transport,GSON Factory和Credential。 不要将最后一个参数(即Credential)留空。

我也得到了类似的GoogleAuthIOExceptionexception:

 com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:286) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) at com.example.MainActivity$ListPartiesTask.doInBackground(MainActivity.java:188) at com.example.MainActivity$ListPartiesTask.doInBackground(MainActivity.java:178) at android.os.AsyncTask$2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask.run(FutureTask.java:234) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:841) Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown at com.google.android.gms.auth.GoogleAuthUtil.zzb(Unknown Source) at com.google.android.gms.auth.GoogleAuthUtil.zza(Unknown Source) at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255) at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279) ... 12 more 

事实certificate我需要使用应用程序调试版本的sha1指纹创建一个客户端ID(我已经为应用程序的签名版本创建了一个,但如果你还没有这样做,你也需要这样做)。

您可以使用以下命令获取指纹(在Mac上,debug.keystore的地址在其他系统上可能不同):

 keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android 

如果您需要获取释放密钥的指纹,您可以这样做:

 keytool -list -v -keystore /PATH/TO/YOUR/release-key.jks -alias RELEASE_KEY_ALIAS