7

誰が私が間違っているのか教えてもらえますか? Google Plus からアクセス トークンを取得する必要があります。

これをメソッドに入れましたonConnected()が、アクセストークンを取得していません。代わりにエラーが発生しています...

コード:

try {
        String token = GoogleAuthUtil.getToken(this, mPlusClient.getAccountName() + "", "oauth2:" + Scopes.PLUS_PROFILE + 
                                "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");
        Log.d("AccessToken", token);
    } catch (UserRecoverableAuthException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GoogleAuthException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

エラー:

08-07 10:10:24.199: E/GoogleAuthUtil(17203): Calling this from your main thread can lead to deadlock and/or ANRs

ユーザーから Google Plus アクセス トークンを取得する正しい方法を誰か教えてもらえますか?

4

4 に答える 4

5

トークンのリクエストをバックグラウンド スレッドに入れる必要があります。この質問でそれを行う方法を示すサンプルコードを投稿しました:

「メインスレッドからこれを呼び出すと、accesToken の取得中にデッドロックや ANR が発生する可能性があります」GoogleAuthUtil (Android での Google Plus 統合) から

于 2013-08-07T08:56:21.903 に答える
2

非同期タスクを使用してフェッチする必要があります。

public void onConnected(Bundle connectionHint) {
// Reaching onConnected means we consider the user signed in. 
Log.i(TAG, "onConnected");

// Update the user interface to reflect that the user is signed in. 
mSignInButton.setEnabled(false); 
mSignOutButton.setEnabled(true); 
mRevokeButton.setEnabled(true); 

// Retrieve some profile information to personalize our app for the user. 
Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);


AsyncTask<Void, Void, String > task = new AsyncTask<Void, Void, String>() {
    @Override 
    protected String doInBackground(Void... params) {
        String token = null;
        final String SCOPES = "https://www.googleapis.com/auth/plus.login ";

        try { 
            token = GoogleAuthUtil.getToken(
                     getApplicationContext(),
                     Plus.AccountApi.getAccountName(mGoogleApiClient), 
                     "oauth2:" + SCOPES);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (GoogleAuthException e) {
            e.printStackTrace();
        } 


      return token;

    } 

    @Override 
    protected void onPostExecute(String token) {
        Log.i(TAG, "Access token retrieved:" + token);
    } 

}; 
task.execute();


System.out.print("email" + email);
mStatus.setText(String.format(
        getResources().getString(R.string.signed_in_as),
        currentUser.getDisplayName()));

Plus.PeopleApi.loadVisible(mGoogleApiClient, null) 
        .setResultCallback(this);

// Indicate that the sign in process is complete. 
mSignInProgress = STATE_DEFAULT; }

アクセス トークンは token 変数に格納されます。

于 2015-05-11T13:45:10.433 に答える