我已根据以下链接将 google plus 登录集成到我的 android 应用程序中
google plus andoid integration
但现在我想检索访问 token ,为此我在 api 控制台中创建了一个项目并为 android 应用程序开发了一个客户端 id,
这就是我检索 token 的方式。但不幸的是我无法获得 token ,它收到 null
String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
String scopes = "oauth2:server:client_id:" + "client id" + ":api_scope:" + scopesString;
也有异常(exception)
03-17 15:20:14.338: W/System.err(8084): com.google.android.gms.auth.GoogleAuthException: Unknown
03-17 15:20:14.338: W/System.err(8084): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
03-17 15:20:14.338: W/System.err(8084): at com.myapp.exemple.NewInstallation$12.doInBackground(NewInstallation.java:600)
03-17 15:20:14.338: W/System.err(8084): at com.myapp.exemple.NewInstallation$12.doInBackground(NewInstallation.java:1)
03-17 15:20:14.348: W/System.err(8084): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-17 15:20:14.348: W/System.err(8084): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-17 15:20:14.348: W/System.err(8084): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-17 15:20:14.348: W/System.err(8084): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-17 15:20:14.348: W/System.err(8084): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-17 15:20:14.348: W/System.err(8084): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-17 15:20:14.348: W/System.err(8084): at java.lang.Thread.run(Thread.java:856)
最佳答案
该字符串(假定“client_id”替换为您的真实客户端 ID)用于检索服务器端离线访问的代码,而不是访问 token 。这将始终要求显示同意对话框,如果您愿意,这是抛出的可恢复异常将执行的操作。
要获取访问 token ,您只需要字符串“oauth2:scope scope”——如果用户从未授予对您的应用的访问权限,这仍然需要同意对话框。我在 https://gist.github.com/ianbarber/9607551 上放了一个示例要点- 相关部分:
String scopes = "oauth2:profile email";
String token = null;
try {
token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
如果您确实想要检索代码,请使用您之前使用的字符串,但请注意每次都需要同意 - 因此如果您想同时在客户端和服务器上登录使用它会很棘手。您可以使用 ID token 来检查服务器是否已经有刷新 token - 我不久前写了一篇关于此的帖子:http://www.riskcompletefailure.com/2013/10/google-android-client-server-sign-in.html - 它使用 PlusClient 而不是 GoogleApiClient,但从一种转换到另一种相对容易。
关于android - Google plus 登录访问 token 检索问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22450924/