William
William

Reputation: 20196

How to use GoogleApiClient to provide credentials for cloud endpoint client

Ok, using the GoogleApiClient of Google Play Services I have let the user select an account (if multiple) and confirm oauth permissions for my Android app. I need this for leader boards and a few other things.

But I'm also using an AppEngine backend and need to authenticate the user with that. To do so I need to pass in the emailAccount with which to authenticate.

Prior to integrating Google Play Services I handled account selection manually, so I always had access to the emailAccount selected by the user. But the GoogleApiClient handles that at arms length.

private void signInToGoogleAccount() {
    googleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API)
        .addScope(Plus.SCOPE_PLUS_LOGIN) // Profile + Circles + ?writing app activities?
        .build();
    googleApiClient.connect();
}

// GPS Connection Callbacks.

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "#onConnected - GoogleApiClient connected!!");
    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
        final Person currentPerson = Plus.PeopleApi.getCurrentPerson(googleApiClient);
        String personName = currentPerson.getDisplayName();
        Log.i(TAG, "name=" + personName);
        String personGooglePlusProfile = currentPerson.getUrl();
        Log.i(TAG, "profileUrl=" + personGooglePlusProfile);
    }
}

@Override
public void onConnectionSuspended(int cause) {
    Log.d(TAG, "#onConnectionSuspended - GoogleApiClient connection suspend. cause=" + cause);
    googleApiClient.connect();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        Log.i(TAG, "#onActivityResult RC_SIGN_IN resultCode=" + resultCode + " data=" + data);
        intentInProgress = false;
        if (resultCode == RESULT_OK) {
            if (!googleApiClient.isConnecting()) {
                googleApiClient.connect();
            }
        } else if (resultCode == RESULT_CANCELED) {
            Log.i(TAG, "#onActivityResult RC_SIGN_IN user cancelled");
        } else {
            Log.w(TAG, "#onActivityResult RC_SIGN_IN something weird");
        }
    }
}

private void doRemoteTask() {
    final GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, AppConstants.AUDIENCE);

    // This is the info that I need to recover from the GooglePlayServices signin.
    credential.setSelectAccountName(userAccountName);

    final MyRemoteTask myRemoteTask = new MyRemoteTask.Builder(
        AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential
    ).build();
    myRemoteTask.doThing(someArg);
}

So what I need to know is :

  1. How do I get the details of the email account selected by the user when I use GoogleApiClient to authorize.
  2. Or is there another more GoogleApiClient friendly way of injecting user identity into an AppEngine Cloud Endpoints client.

Upvotes: 5

Views: 2953

Answers (1)

William
William

Reputation: 20196

Amazing how writing the question out for others can help unblock thought processes.

Solution is use Plus.AccountApi.getAccountName(googleApiClient); Eg

@Override
public void onConnected(Bundle bundle) {
    final String accountName = Plus.AccountApi.getAccountName(googleApiClient);
    Log.i(TAG, "#onConnected - GoogleApiClient accountName=" + accountName);
}

Upvotes: 3

Related Questions