7

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;
}
Jason
  • 1,238
  • 1
  • 15
  • 18

2 Answers2

3

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

        }
dd619
  • 5,910
  • 8
  • 35
  • 60
  • Thanks, but there are a few things that don't work for me with this answer: 1. Calling this from onCreate() will not work, since the user needs to be authenticated in order to use mPlusClient.. 2. In the link I have above, this is pretty much the solution they have listed for the "online" token. I'm trying to get an "offline" token that I can verify on my server. I have a variation of what you have listed above, and it works fine, I get a token, but it's not the type of token I need (online as opposed to offline). – Jason Jul 24 '13 at 17:55
  • can you please help me on this bug http://stackoverflow.com/questions/21445265/google-coordinate-authentification – haythem souissi Jan 30 '14 at 00:03
  • upvoting for the LOC: String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE; – Mingsheng Jul 04 '15 at 10:24
1

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;
Lee
  • 3,972
  • 22
  • 17
  • Sorry, my bad. Looks like I removed that part when I posted it, I have ":api_scope:" in my code. I've updated the post to show that. – Jason Jul 24 '13 at 17:57
  • Ahhh...I missed separating out the scopeString at the end. I made that change and now I get a "UserRecoverableAuthException: NeedPermission" error. I tried handling that, but now I'm basically having [this problem](http://stackoverflow.com/questions/17713435/android-google-integration-repeated-userrecoverableauthexception). I'll mark this as correct, since it resolved the initial problem I was having...stupid late night mistake ;-) – Jason Jul 24 '13 at 21:53
  • Since when did the GoogleAuthUtil.getToken() method start throwing a GoogleAuthException with message "BadUsername" for an email that is not registered on a device, instead of the previous exception IllegalArgumentException with message "Non existing account 'email_address'" ? http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html#getToken(android.content.Context , java.lang.String, java.lang.String) – Etienne Lawlor Nov 06 '13 at 02:29
  • @Jason did u get the token from this solution ?? – Mr.G Mar 17 '14 at 12:45
  • @Mr.G It's been a while, but looking at my current code (which works and gets a token), I don't use the appActivities and I generated the scope string a little differently. – Jason Mar 21 '14 at 22:01
  • @Jason i found a way to retrieve the token , thank BTW – Mr.G Mar 22 '14 at 03:36