“在获取 accesToken 时,从主线程调用它可能会导致死锁和/或 ANR"来自 GoogleAuthUtil(Android 中的 Google Plus 集成) [英] "Calling this from your main thread can lead to deadlock and/or ANRs while getting accesToken" from GoogleAuthUtil(Google Plus integration in Android)
问题描述
在我的 android 应用程序中,我试图从 GoogleAuthUtil 获取 AccessToken,如下所示:
<块引用>accessToken = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName(), "oauth2:" + SCOPES);
但是在这一行我得到如下错误:
<块引用><块引用>E/GoogleAuthUtil(4696):从主线程调用它会导致死锁和/或 ANRE/GoogleAuthUtil(4696): java.lang.IllegalStateException: 从主线程调用它会导致死锁E/GoogleAuthUtil(4696):位于 com.google.android.gms.auth.GoogleAuthUtil.b(来源不明)E/GoogleAuthUtil(4696):在 com.google.android.gms.auth.GoogleAuthUtil.getToken(来源不明)E/GoogleAuthUtil(4696):位于 com.google.android.gms.auth.GoogleAuthUtil.getToken(来源不明)
这个问题有什么解决办法吗?任何帮助将不胜感激.
试试这样的 AsyncTask:
AsyncTasktask = new AsyncTask() {@覆盖受保护的字符串 doInBackground(Void... params) {字符串标记 = null;尝试 {令牌 = GoogleAuthUtil.getToken(MainActivity.this,mGoogleApiClient.getAccountName(),oauth2:"+ 范围);} 捕捉(IOException瞬态Ex){//网络或服务器错误,稍后再试Log.e(TAG,transientEx.toString());} catch (UserRecoverableAuthException e) {//恢复(使用 e.getIntent())Log.e(TAG, e.toString());意图恢复 = e.getIntent();startActivityForResult(恢复,REQUEST_CODE_TOKEN_AUTH);} 捕获(GoogleAuthException authEx){//调用永远不会成功//假设你已经验证过了//Google Play 服务已安装.Log.e(TAG, authEx.toString());}返回令牌;}@覆盖protected void onPostExecute(String token) {Log.i(TAG, "检索到的访问令牌:" + 令牌);}};任务.执行();
SCOPES
是一个以空格分隔的 OAuth 2.0 范围字符串列表.例如 SCOPES
可以定义为:
public static final String SCOPES = "https://www.googleapis.com/auth/plus.login "+ "https://www.googleapis.com/auth/drive.file";
这些代表您的应用向用户请求的权限.本示例中请求的范围记录在此处:
In my android application, I am trying to get AccessToken from GoogleAuthUtil as below :
accessToken = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName(), "oauth2:" + SCOPES);
But At this line I am gettting error as below :
E/GoogleAuthUtil(4696): Calling this from your main thread can lead to deadlock and/or ANRs E/GoogleAuthUtil(4696): java.lang.IllegalStateException: calling this from your main thread can lead to deadlock E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.b(Unknown Source) E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) E/GoogleAuthUtil(4696): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
Any solution of this problem? Any help will be appreciated.
Try it with an AsyncTask like this:
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(
MainActivity.this,
mGoogleApiClient.getAccountName(),
"oauth2:" + SCOPES);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
Intent recover = e.getIntent();
startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
Log.i(TAG, "Access token retrieved:" + token);
}
};
task.execute();
SCOPES
is a space separated list of OAuth 2.0 scope strings. For example SCOPES
could be defined as:
public static final String SCOPES = "https://www.googleapis.com/auth/plus.login "
+ "https://www.googleapis.com/auth/drive.file";
These represent the permissions that your app is requesting from the user. The scopes requested in this example are documented here:
这篇关于“在获取 accesToken 时,从主线程调用它可能会导致死锁和/或 ANR"来自 GoogleAuthUtil(Android 中的 Google Plus 集成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!