Silent sign in to retrieve token with GoogleApiClient

后端 未结 3 2113
难免孤独
难免孤独 2020-12-05 14:08

I am using \"Google Sign-In\" in my app. Hence I use the class GoogleApiClient to get the user email and the ID token that I need for my backend.

When the user signs

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 14:46

    Yes, answer above is correct. In general, any GoogleApiClient needs to be connected before it can return you any data. enableAutoManage helps you to call connect() / disconnect() automatically during onStart() / onStop(). If you don't use autoManage, you will need to connect() manually.

    And even better, you should disconnect in a finally block.

    Assuming you are not on the UI thread.

    try {
        ConnectionResult result = mGoogleApiClient.blockingConnect();
        if (result.isSuccess()) {
            GoogleSignInResult googleSignInResult =
                Auth.GoogleSignInApi.silentSignIn(googleApiClient).await();
        ...
        }
    } finally {
        mGoogleApiClient.disconnect();
    }
    

    And also, to clean up your code a little bit: 1. gso built from below configuration is identical to your pasted code above:

    GoogleSignInOptions gso =
       new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(SERVER_CLIENT_ID)
            .requestEmail()
            .build();
    
    1. Based on your current logic, addOnConnectionFailedListener / addConnectionCallbacks doesn't help other than adb log. Maybe just remove them completely?

提交回复
热议问题