获取令牌代码时消息NeedPermission

时间:2014-03-13 06:49:58

标签: android oauth

我使用以下代码段来获取令牌:

private class task extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        Bundle appActivities = new Bundle();
        appActivities.putString(
                GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
                Constants.ADD_ACTIVITY_SCHEME + " "
                        + Constants.BUY_ACTIVITY_SCHEME);

        String serverClientID = "My_Client_Id";
        String scopes = "oauth2:server:client_id:" + serverClientID
                + ":api_scope:" + Scopes.PLUS_LOGIN + " "
                + Scopes.PLUS_PROFILE;
        String code = null;
        try {

            code = GoogleAuthUtil.getToken(MainActivity.this, // Context
                                                                // context
                    mPlusClient.getAccountName(), // String accountName
                    scopes, // String scope
                    appActivities // Bundle bundle
                    );

        } catch (IOException transientEx) {
            code = "Loi 1";
        } catch (UserRecoverableAuthException e) {
            code = "Loi 2: "+e.getMessage();
        } catch (GoogleAuthException authEx) {
            code = "Loi 3";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return code;
    }

    @Override
    protected void onPostExecute(String token) {
        showToast(token);
    }
}

我在onConnected方法中执行这行代码:

new task.execute();

发生UserRecoverableAuthException并且我的toast显示消息:“NeedPermission”。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

您是否在清单中添加了以下权限?

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

这是我的工作代码!

    @Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    String accountName = mPlusClient.getAccountName();
    // Get user's information

    task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String token = null;

            try {
                token = GoogleAuthUtil.getToken(MyNetwork.this,
                        mPlusClient.getAccountName(), "oauth2:"
                                + Scopes.PROFILE);
                Log.i("TAG", "token" + token);
            } 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) {

                Log.e(TAG, authEx.toString());
            }
            return token;
        }

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

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mHandler.sendEmptyMessage(SHOW_PROGRESS);
        }

    };
    task.execute();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
            .show();

}

我正在获取以下用户信息。

/**
 * Fetching user's information name, email, profile pic
 * */
private void getProfileInformation(String mToken) {

    String mAccessToken = mToken == null ? "" : mToken;
    String mProfileId = "";
    String mProfileName = "";
    String mImageUrl = "";
    String mSecretKey = "";

    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {

            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);

            txtGooglePlus.setText(currentPerson.getDisplayName());
            mProfileName = currentPerson.getDisplayName();
            mImageUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            Log.e(TAG, "Name: " + mProfileName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + mImageUrl);
            mProfileId = currentPerson.getId();


        } else {
            Toast.makeText(getApplicationContext(),
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这对我来说很好。测试。检查并随时询问是否有任何问题。