Android 安卓后台服务中的无声谷歌登录

Android 安卓后台服务中的无声谷歌登录,android,android-service,google-api-client,google-signin,Android,Android Service,Google Api Client,Google Signin,我正在android应用程序中运行后台服务。我使用从登录活动获得的IdToken在后端服务器上进行身份验证。该服务正在START\u STICKY模式下运行,因此即使应用程序关闭,该服务仍会在后台运行,以从后端服务器获取任何通知。我面临的问题是,当IdToken过期时,我无法在服务本身中续订它。如果令牌已过期,则回调函数不会收到任何结果。如果令牌尚未过期,它会立即获得结果 以下是signIn函数和handleSignIn函数的代码 private void signIn() {

我正在android应用程序中运行后台服务。我使用从登录活动获得的IdToken在后端服务器上进行身份验证。该服务正在
START\u STICKY
模式下运行,因此即使应用程序关闭,该服务仍会在后台运行,以从后端服务器获取任何通知。我面临的问题是,当IdToken过期时,我无法在服务本身中续订它。如果令牌已过期,则回调函数不会收到任何结果。如果令牌尚未过期,它会立即获得结果

以下是signIn函数和handleSignIn函数的代码

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);
        if (opr.isDone()) {
            // If the user's cached credentials are valid, the OptionalPendingResult will be "done"

            // and the GoogleSignInResult will be available instantly.
            Log.d(TAG, "Got cached sign-in");
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            // If the user has not previously signed in on this device or the sign-in has
            // expired,
            // this asynchronous branch will attempt to sign in the user silently.  Cross-device
            // single sign-on will occur in this branch.
            Log.d(TAG, "had to sign in again");
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(GoogleSignInResult googleSignInResult) {
                    Log.d(TAG, "got result");
                    handleSignInResult(googleSignInResult);
                }
            });
        }}}).start();
    }
private void signIn(){
新线程(new Runnable(){public void run()){
GoogleSignenOptions gso=新建GoogleSignenOptions.Builder(GoogleSignenOptions.DEFAULT\u登录)
.requestEmail()
.requestIdToken(“”)
.build();
GoogleAppClient mgoogleAppClient=新的GoogleAppClient.Builder(上下文)
.addApi(Auth.GOOGLE\u SIGN\u IN\u API,gso)
.build();
OptionalPendingreult opr=Auth.GoogleSignInApi.silentSignIn
(mGoogleApiClient);
if(opr.isDone()){
//如果用户的缓存凭据有效,则OptionalPendingreult将“完成”
//谷歌搜索结果将立即发布。
Log.d(标记“已缓存登录”);
GoogleSignInResult结果=操作获取();
handleSignInResult(结果);
}否则{
//如果用户以前没有在此设备上登录,或者登录失败
//过期,
//此异步分支将尝试以静默方式登录用户。跨设备
//此分支中将发生单点登录。
Log.d(标签“必须再次登录”);
操作setResultCallback(新的ResultCallback(){
@凌驾
公开作废结果(GoogleSignInResult GoogleSignInResult){
Log.d(标记“got result”);
handleSignInResult(谷歌SignInResult);
}
});
}}}).start();
}
尝试移动

opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                Log.d(TAG, "got result");
                handleSignInResult(googleSignInResult);
            }
        });

同样的问题,这里是我目前的发现

在令牌过期之前,登录过程实际上并不使用GoogleAPI客户端

如果令牌过期,则使用Google API客户端,在调用silentSignIn()时需要连接该客户端。您可以使用blockingConnect()手动连接,也可以使用enableAutoManage()自动连接(如果您在活动中)

因此,在您的情况下,看起来您至少应该连接客户端

但还有更多。。。如果ID令牌过期,则登录过程涉及接收活动结果,您只能在活动中执行,而不能在服务中执行

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();
}