"从你的主线程中调用,这可能导致死锁和/或ANRS同时获得accesToken"从GoogleAuthUtil(谷歌加集成在Android中)死锁、你的、这可、主线

2023-09-12 22:39:04 作者:我只拥你

在我的Andr​​oid应用程序,我想从GoogleAuthUtil获得AccessToken如下:

  

accessToken = GoogleAuthUtil.getToken(这一点,mPlusClient.getAccountName(),oauth2:+范围);

但是,在这条线,我gettting错误如下:

       

E / GoogleAuthUtil(4696):调用该从你的主线程可能导致死锁和/或ANRS     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(来源不明)

  

这个问题的任何解决方案?任何帮助将AP preciated。

解决方案

有这样一个AsyncTask的尝试:

 的AsyncTask<虚空,虚空,字符串>任务=新的AsyncTask<虚空,虚空,字符串>(){
            @覆盖
            保护字符串doInBackground(空... PARAMS){
                字符串标记= NULL;

                尝试 {
                    令牌= GoogleAuthUtil.getToken(
                            MainActivity.this,
                            mGoogleApiClient.getAccountName(),
                            oauth2:+范围);
                }赶上(IOException异常transientEx){
                    //网络或服务器错误,请稍后重
                    Log.e(TAG,transientEx.toString());
                }赶上(UserRecoverableAuthException E){
                    //恢复(与e.getIntent())
                    Log.e(TAG,e.toString());
                    意图恢复= e.getIntent();
                    startActivityForResult(恢复,REQUEST_ code_TOKEN_AUTH);
                }赶上(GoogleAuthException authEx){
                    //电话不意想不到的成功
                    //假设您已经验证
                    //谷歌播放服务的安装。
                    Log.e(TAG,authEx.toString());
                }

                返回令牌;
            }

            @覆盖
            保护无效onPostExecute(字符串标记){
                Log.i(TAG,访问令牌检索:+令牌);
            }

        };
        task.execute();
 

范围是的OAuth 2.0范围字符串的空格分隔列表。例如范围可以定义为:

 公共静态最后弦乐范围=htt​​ps://www.googleapis.com/auth/plus.login
    +https://www.googleapis.com/auth/drive.file;
 
java线程死锁的定位方法

这些重新present你的应用程序从用户请求的权限。在这个例子中所要求的范围被记录在这里:

https://developers.google.com/+/api/oauth https://developers.google.com/drive/android/files

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:

https://developers.google.com/+/api/oauth https://developers.google.com/drive/android/files