Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google+ unable to get auth code

I'm trying to get a Google+ auth code based on this article: Server-side access for your app. However, whenever I run my app and try to get the code I get the following in LogCat:

07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown

I browsed a lot of stuff and from what I can tell I have everything setup correctly in the code below. Any ideas why it's not getting an auth token?

FYI, I have one project setup in the API console that has both the app and web applications configured for oauth and I'm using the server (not the app) ID for the scopes string. I've also followed the example(s) from the above link and the app works fine and allows you to sign-in/out, but I just can't get the auth code.

private String getAccessToken() {
    String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
    String scope = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopesString;
    Bundle appActivities = new Bundle();
    appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
            "http://schemas.google.com/AddActivity");
    String code = null;
    try {
         code = GoogleAuthUtil.getToken(
                    this,                             // Context context
                    mPlusClient.getAccountName(),     // String accountName
                    scopes,                           // String scope
                    appActivities                     // Bundle bundle
                );
    } catch (IOException transientEx) {
      // network or server error, the call is expected to succeed if you try again later.
      // Don't attempt to call again immediately - the request is likely to
      // fail, you'll hit quotas or back-off.
        Log.e("getAccessToken()", "[ERROR] IOException: " + transientEx);
        return null;
    } catch (UserRecoverableAuthException e) {
           // Recover
        Log.e("getAccessToken()", "[ERROR] UserRecoverableAuthException: " + e);
        code = null;
    } catch (GoogleAuthException authEx) {
      // Failure. The call is not expected to ever succeed so it should not be
      // retried.
        Log.e("getAccessToken()", "[ERROR] GoogleAuthException: " + authEx);
      return null;
    } catch (Exception e) {
        Log.e("getAccessToken()", "[ERROR] Exception: " + e);
      throw new RuntimeException(e);
    }
    return code;
}
like image 281
Jason Avatar asked Jul 24 '13 07:07

Jason


2 Answers

try this,

in you onCreate() write,

new GetTokenAsync.execute();

and then write AsyncTask to get access token as,

public class GetTokenAsync extends AsyncTask<String, String, String>
        {

            @Override
            protected void onPreExecute()
                {

                    super.onPreExecute();
                    plusUtilities.ShowProgressDialog("Getting Token...");

                }

            @Override
            protected String doInBackground(String... params)
                {
                    String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
                    try
                        {
                            // We can retrieve the token to check via
                            // tokeninfo or to pass to a service-side
                            // application.
                            String token = GoogleAuthUtil.getToken(getParent(), mPlusClient.getAccountName(), scope);
                            Log.i("OAUTH Token:", token);
                            Log.i("Scope:", "" + scope);
                            Log.i("GOOGLE_ACCOUNT_TYPE", "" + GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

                            Person currentperson = mPlusClient.getCurrentPerson();
                               Log.i("person", currentperson.toString());

                        }
                    catch (UserRecoverableAuthException e)
                        {
                            Log.e("UserRecoverableAuthException", "UserRecoverableAuthException");
                            e.printStackTrace();
                        }
                    catch (IOException e)
                        {
                            Log.e("IOException", "IOException");
                            e.printStackTrace();
                        }
                    catch (GoogleAuthException e)
                        {
                            Log.e("GoogleAuthException", "GoogleAuthException");
                            e.printStackTrace();
                        }
                    catch (Exception e)
                        {
                            Log.e("Exception", "Exception");
                            e.printStackTrace();
                        }
                    return null;
                }

            @Override
            protected void onPostExecute(String result)
                {

                    super.onPostExecute(result);
                    plusUtilities.DissmissProgressDialog();
                }

        }
like image 109
dd619 Avatar answered Nov 15 '22 05:11

dd619


I don't know if you modified the line to post the question but looking at the code you posted, this line is wrong:

String scopes = "oauth2:server:client_id:<My server client ID>:scopesString";

It should be:

String scopes = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopeString;
like image 1
Lee Avatar answered Nov 15 '22 04:11

Lee