message NeedPermission when get token code

不问归期 提交于 2019-12-23 03:24:06

问题


I use the following snippet to get token:

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);
    }
}

I execute this line of code in onConnected method:

new task.execute();

UserRecoverableAuthException occur and my toast show message: "NeedPermission".

How can i fix it?


回答1:


Have your added the following permission in your manifest?

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

here is the working code for me!

    @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();

}

and i am getting user information like the following.

/**
 * 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();
    }
}

It is working fine for me. Tested. Check and feel free to ask if there is any issue.



来源:https://stackoverflow.com/questions/22371157/message-needpermission-when-get-token-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!