来自GoogleAuthUtil(Android中的Google Plus集成)的“从主线程调用此代码可能会导致死锁和/或ANR,同时获取accesToken”。

蓬亭

在我的Android应用程序中,我试图从GoogleAuthUtil获取AccessToken,如下所示:

accessToken = GoogleAuthUtil.getToken(this,mPlusClient.getAccountName(),“ oauth2:” + SCOPES);

但是在这一行,我得到如下错误:

E / GoogleAuthUtil(4696):从您的主线程调用可能导致死锁和/或ANR E / 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尝试一下:

        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是OAuth 2.0作用域字符串的以空格分隔的列表。例如SCOPES,可以定义为:

public static final String SCOPES = "https://www.googleapis.com/auth/plus.login "
    + "https://www.googleapis.com/auth/drive.file";

这些代表您的应用向用户请求的权限。此示例中要求的范围记录在这里:

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章