Lanbo
Lanbo

Reputation: 15692

GoogleAuthException "Unknown" When Trying to get Token

I'm trying to get an authorization code via Google Plus login to use it on the server. The G+ login works, I am logged in on the device, but when I try to get the Token, I get an exception with the super helpful message Unknown. This is the code:

private class GoogleLoginTask extends AsyncTask<Void, Void, String> {
    private static final String LOGIN_SCOPES = 
        "https://www.googleapis.com/auth/plus.login " +
        "https://www.googleapis.com/auth/userinfo.email";

    @Override
    protected String doInBackground(Void[] params) {
        String apiKey = getApplicationMetaData("google.client-id");
        String scopes = "oauth2:server:client_id:" + apiKey
                      + ":api_scope:" + LOGIN_SCOPES;

        Bundle appActivities = new Bundle();
        appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES, 
                                "http://schema.org/AddAction");
        String code = null;

        try {
            code = GoogleAuthUtil.getToken(getBaseContext(),
                  getPlusClient().getAccountName(), scopes, appActivities);

        } catch (IOException e) {
            Toast.makeText(getBaseContext(), R.string.error_network, Toast.LENGTH_LONG).show();
        } catch (UserRecoverableAuthException e) {
            startActivityForResult(e.getIntent(), AUTH_CODE_REQUEST_CODE);
        } catch (GoogleAuthException e) {
            Log.e(TAG, "Google auth exception: ", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return code;
    }
}

I have taken the code directly from the Developer Page. Though the docs don't clarify about many of the parameters, I think I have it correctly.

I have triple checked the apiKey and the getPlusClient().getAccountName() contents, they're correct. And I made sure the application and the server are in the same project on the API console.

No idea what else I could be doing wrong.

Upvotes: 2

Views: 1032

Answers (1)

Lanbo
Lanbo

Reputation: 15692

I have found the problem: The build.gradle file had a slightly (typoed) different valued for applicationId than what was set in the API-Console.

Though some simple error message in this case would have been great.

Upvotes: 4

Related Questions