1

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?

  • I do follow @Rrthinavel code, i received an id_token like this: ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI... I pass this id_token into google url lis=ke this: https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI... And i get a messeage that: {"error": "invalid_token","error_description": "Invalid Value"} I think may be exist difference between getToken() 3 args and getToken() 4 args. When i code follow you, i get the token code, but invalid when verify. I try code by my code above, i get an exception: NeedPermission. Please help me! THank you. – Nguyễn Ngọc Hoàng Mar 16 '14 at 03:46
  • Any help. I need to fix this. Thank you. – Nguyễn Ngọc Hoàng Mar 17 '14 at 08:04
  • @Nguyen Try this URL : https://graph.facebook.com/me?access_token="Your access token" – Rethinavel Mar 17 '14 at 08:34

1 Answers1

0

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.

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
  • I have all need permssion in AndroidManifest.xml, like here. ' ' – Nguyễn Ngọc Hoàng Mar 13 '14 at 08:05
  • Please tell me what is REQUEST_CODE_TOKEN_AUTH and it's value. Thank you! – Nguyễn Ngọc Hoàng Mar 13 '14 at 08:08
  • It is any integer value to identify the request. in my case, It is. `private static final int REQUEST_CODE_TOKEN_AUTH = 100; ` – Rethinavel Mar 13 '14 at 08:22
  • does have difference between your 'scopes' and following scope: String scopes = "oauth2:server:client_id::api_scope: "; – Nguyễn Ngọc Hoàng Mar 13 '14 at 08:29
  • @Rrthinavel: I do follow your code, i received an id_token like this: ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI... I pass this id_token into google url lis=ke this: https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI... And i get a messeage that: {"error": "invalid_token","error_description": "Invalid Value"} I think may be exist difference between getToken() 3 args and getToken() 4 args. When i code follow you, i get the token code, but invalid when verify. I try code by my code above, i get an exception: NeedPermission. Please help me! THank you. – Nguyễn Ngọc Hoàng Mar 16 '14 at 03:44