Google-plus: unable to load person

7

I want to have Google Plus login in my application. I could connect but unable to get current user's profile information

@Override
public void onConnected() {

    String accountName = mPlusClient.getAccountName();

    mPlusClient.loadPerson(this, "me");
}

@Override
public void onPersonLoaded(ConnectionResult status, Person person) {

    Log.d("onPersonLoaded - ConnectionResult", "" + status);
    Log.d("onPersonLoaded - person", "" + person);
    if (status.getErrorCode() == ConnectionResult.SUCCESS) {
        Log.d(TAG, "Display Name: " + person.getDisplayName());
    }
}

I could get the Account Name in onConnected But onPersonLoaded gives null for person

The logs show:

onPersonLoaded - ConnectionResult(29861): ConnectionResult{statusCode=NETWORK_ERROR, resolution=null}
onPersonLoaded - person(29861): null

From the docs here

public static final int NETWORK_ERROR

A network error occurred. Retrying should resolve the problem.

Constant Value: 7 (0x00000007)

But I get the same error no matter how many times I retry. And these are the permission I use:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />

I have no idea how to resolve this and been searching for the past 2 hours. Any help is highly appreciated, thank you.

android
google-plus
asked on Stack Overflow Mar 4, 2013 by user1537779 • edited Mar 4, 2013 by user1537779

2 Answers

13

finally i found the answer . :)

loadPerson() and loadPeople() will return you expected data only in exported APK signed with valid key . by valid here i mean it should match with SHAI and other specification of your API key console .

one more thing you could missed when following sample code is define scope in PlusClient constructor, like

mPlusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PLUS_LOGIN)
                .setVisibleActivities("http://schemas.google.com/AddActivity").build();
answered on Stack Overflow Mar 14, 2013 by Shailendra Singh Rajawat • edited Aug 7, 2015 by Shailendra Singh Rajawat
4
private class connectAsyncTask extends AsyncTask<String, Void, String> {

    String sAccessToken = null;

    @Override
    protected void onPostExecute(String info) {

        Log.d("JSONData = ", "" + info);
        //This contains all the data you wanted.
    }

    @Override
    protected String doInBackground(String... params) {

        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL(
                    "https://www.googleapis.com/oauth2/v1/userinfo");
            sAccessToken = GoogleAuthUtil
                    .getToken(
                            MainActivity.this,
                            mPlusClient.getAccountName() + "",
                            "oauth2:"
                                    + Scopes.PLUS_PROFILE
                                    + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");

            //This is probably not the right way to do.
            //What I am doing here is, get an AccessToken, invalidate it and get a new
            //AccessToken again. Because I couldn't find a way to check whether the 
            //AccessToken is expired or not.

            GoogleAuthUtil.invalidateToken(MainActivity.this, sAccessToken);

            sAccessToken = GoogleAuthUtil
                    .getToken(
                            MainActivity.this,
                            mPlusClient.getAccountName() + "",
                            "oauth2:"
                                    + Scopes.PLUS_PROFILE
                                    + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Bearer "
                    + sAccessToken);

            BufferedReader r = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream(), "UTF-8"));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            line = total.toString();
            if (!TextUtils.isEmpty(line)) {
                return line;
            } else {
                return null;
            }
        } catch (UserRecoverableAuthException userAuthEx) {
            // Start the user recoverable action using the intent returned
            // by getIntent()

            userAuthEx.printStackTrace();
            mActivity.this.startActivityForResult(
                    userAuthEx.getIntent(), MY_ACTIVITYS_AUTH_REQUEST_CODE);
            return null;
        } catch (FileNotFoundException e) {
            //You get this exception when the AccessToken is expired.
            //I don't know how its a FileNotFoundException
            //Thats why instead of relying on this, I did the above to get
            //new AccessToken
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

    @Override
    protected void onPreExecute() {
    }
}
answered on Stack Overflow Mar 14, 2013 by user1537779

User contributions licensed under CC BY-SA 3.0