我已根据以下链接将google plus登录到我的Android应用程序
google plus andoid integration
但是现在我想要检索访问令牌,因为我已经在api控制台中创建了一个项目并为android应用程序开发了一个客户端ID,
这是我检索令牌的方法。但不幸的是我无法获得令牌,它收到了空白
String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
String scopes = "oauth2:server:client_id:" + "client id" + ":api_scope:" + scopesString;
也有例外
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)
答案 0 :(得分:1)
该字符串(假设“client_id”替换为您的真实客户端ID)用于检索服务器端脱机访问的代码,而不是访问令牌。这将始终需要显示同意对话框,如果您愿意,这将抛出可恢复的异常。
要获取访问令牌,您只需要字符串“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令牌来检查服务器是否已经有刷新令牌 - 我写了一篇关于它的帖子:http://www.riskcompletefailure.com/2013/10/google-android-client-server-sign-in.html - 它使用的是PlusClient而不是GoogleApiClient,但它相对容易翻译从一个到另一个。