I am running a background service in my android app. I use the IdToken that I get from the sign in activity to authenticate at the backend server. The service is running in
Try moving the
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
Log.d(TAG, "got result");
handleSignInResult(googleSignInResult);
}
});
to before its if statement. I know the google docs say to have it the way you do and I did the same until I realized that the ResultCallback will never get called if it only gets initialized in the else block, after opr has already run.
Your code should look like this after following my suggestion:
private void signIn() {
new Thread(new Runnable() { public void run() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken("<My server_client_id>")
.build();
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn
(mGoogleApiClient);
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
Log.d(TAG, "got result");
handleSignInResult(googleSignInResult);
}
});}}).start();
}
Got the same issue, so here are my findings so far.
Before the token expires, the sign-in process does not actually use the Google API client.
If the token is expired, the Google API client is used and it needs to be connected when calling silentSignIn(). You can connect it manually with blockingConnect(), or automatically with enableAutoManage() (if you are in an activity).
So in your case it looks like you should at least connect the client.
But there's more... If the ID token is expired, the sign-in process involves receiving an activity result, which you can only do in an activity, not a service.