3

When I try to connect to Google coordinate, I always get an exception GoogleAuthException.

I have a Google Maps Coordinate license. I did create my client Id in google console with my package application name and my SHA1.

I added the permissions to my manifest file:

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

I use this code:

final String SCOPE = "oauth2:https://www.googleapis.com/auth/coordinate";
try {
    mGoogleCoordinatetoken = GoogleAuthUtil.getToken(activity, email, SCOPE);
    Log.e(getClass().getSimpleName(), "token ="+mGoogleCoordinatetoken);
} catch (GooglePlayServicesAvailabilityException playEx) {
    Log.e(getClass().getSimpleName(), "GooglePlayServicesAvailabilityException "+playEx.getMessage());
} catch (UserRecoverableAuthException userAuthEx) {
    // Start the user recoverable action using the intent returned by
    // getIntent()
    Log.e(getClass().getSimpleName(), "UserRecoverableAuthException "+userAuthEx.getMessage());
} 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(getClass().getSimpleName(), "IOException "+transientEx.getMessage());
} catch (GoogleAuthException authEx) {
    // Failure. The call is not expected to ever succeed so it should not be
    // retried.
    Log.e(getClass().getSimpleName(), "GoogleAuthException "+authEx.getMessage());
}

Any idea how I can fix this exception?

Exception:

01-30 22:24:53.968: E/getAccessToken()(24800): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown
01-30 22:24:53.998: E/AccessTokenTask(24800): mGoogleCoordinatetoken =null
haythem souissi
  • 3,263
  • 7
  • 50
  • 77
  • If you need token only to further verify at your server backend then you can use "String SCOPE = "audience:server:client_id:" +Constants.WED_CLIENT_ID;" as your scope – Shikha Shah Jan 30 '14 at 15:16
  • And if you are going to use scope "oauth2:https://www.googleapis.com/auth/coordinate" then you have to go same route as Madhur Ahuja Suggested..Bacuase this scope just doesn't give you token..it first needs user authorization..only this scope "audience:server:client_id:" directly gives you token which is called cross-client identity – Shikha Shah Jan 30 '14 at 15:20
  • i want to get list of jobs for example: https://developers.google.com/coordinate/v1/jobs/list , I don't need the token to do that? – haythem souissi Jan 30 '14 at 16:05
  • Can you paste the exception? – Shikha Shah Jan 30 '14 at 16:29
  • It clearly says that this requires Authorization..you need to try "https://www.googleapis.com/auth/coordinate" with your other scope..try this http://stackoverflow.com/questions/17827256/android-google-unable-to-get-auth-code – Shikha Shah Jan 30 '14 at 16:48
  • i edited my question and added exception message: GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown – haythem souissi Jan 30 '14 at 21:26

3 Answers3

4
    String WEB_APPLICATION_CLIENT_ID = "656631023202-9jsg9faqe87n1uo7f5g6iupti1jl2nps.apps.googleusercontent.com";
    String scopes = String.format("audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID );

    Log.e(getClass().getSimpleName(), "email ="+email);

    String code = null;
    try {
         code = GoogleAuthUtil.getToken(
                 LoginActivity.this,                             // Context context
                 email,     // String accountName
                    scopes
                );
         mGoogleCoordinatetoken = code;
    } 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);
    } 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);
    } catch (Exception e) {
        Log.e("getAccessToken()", "[ERROR] Exception: " + e);
      throw new RuntimeException(e);
    }
haythem souissi
  • 3,263
  • 7
  • 50
  • 77
4

I had the exact same problem and resolved by modifying the scope to :

"oauth2:https://www.googleapis.com/auth/[API YOU WANT]"

(the beggining is very important !)

Hope it will help someone :)

user3885760
  • 53
  • 1
  • 5
1

The question is why do you need to get a token. As you commented in my question, you should be fine with GoogleAccountCredential object. Once you have the credential object, you can make calls to Google APIs

  credential = GoogleAccountCredential.usingOAuth2(this, scopes);
                if (TextUtils.isEmpty(appPreferences.getUserName()))
                {
                        try
                        {

                                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
                        }
                        catch (ActivityNotFoundException e)
                        {

                                Toast.makeText(this, getString(R.string.gps_missing), Toast.LENGTH_LONG).show();

                                return;
                        }

                }
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • i want to get list of jobs for example: https://developers.google.com/coordinate/v1/jobs/list I don't need the token to do that? – haythem souissi Jan 30 '14 at 15:28
  • https://www.googleapis.com/coordinate/v1/teams/ncXej4BPeb9VGjSENjvOuw/jobs?key=AIzaSyAfC-L5yMj37yzsvGHpIytbkvJw83NJqpE I am getting error login required – haythem souissi Jan 30 '14 at 16:14