简体   繁体   English

Android Google登录

[英]Android Google Sign-In

I need to enable server-side access to Google Drive. 我需要启用服务器端对Google云端硬盘的访问。 In this case a person is using his Android device. 在这种情况下,一个人正在使用他的Android设备。 As far as I understood the steps are as follows: 1. Create GoogleSignInOptions 2. Using the GoogleSignInOptions create GoogleSignInAccount 3. Getting authCode from GoogleSignInAccount 4. Exchange the authCode for access/refresh/ID tokens 据我了解,步骤如下:1.创建GoogleSignInOptions 2.使用GoogleSignInOptions创建GoogleSignInAccount 3.从GoogleSignInAccount获取authCode 4.交换authCode以获得访问/刷新/ ID令牌

I am stuck on step 3. I followed the well-described tutorials without any success - https://developers.google.com/identity/sign-in/android/offline-access , https://developers.google.com/identity/sign-in/android/sign-in#configure_google_sign-in_and_the_googleapiclient_object 我被困在第3步。我遵循了精心描述的教程,但没有成功-https: //developers.google.com/identity/sign-in/android/offline-access,https : //developers.google.com/身份/登录/安卓/登入#configure_google_sign-in_and_the_googleapiclient_object

Here is the code that initialize sign-in process: 以下是初始化登录过程的代码:

final GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
                .requestServerAuthCode(backend_server_web_client_id)
                .build();

GoogleSignInClient google_api_client = GoogleSignIn.getClient(context, gso);

activity.startActivityForResult(google_api_client.getSignInIntent(), RC_SIGN_IN);

Here is the code that handles the sign-in result: 以下是处理登录结果的代码:

// data is the intent from onActivityResult callback
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

if (task.isComplete())
    handle(task);
else {
    task.addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
            @Override
            public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                handle(task);
            }}
    });
}

And finally here is the handle function where is the problem: 最后是问题的句柄函数:

public void handle(Task<GoogleSignInAccount> task) {
    try {
        GoogleSignInAccount account = task.getResult(ApiException.class);
    } catch (ApiException e) {
    //I'm always getting this exception with status code 10, which means DEVELOPER ERROR. Keys in Google API console are checked multiple times.
    }
}

In handle function I'm always getting an exception with status code 10, which means DEVELOPER_ERROR. 在句柄函数中,我总是会收到状态码为10的异常,这意味着DEVELOPER_ERROR。 Keys in Google API console are checked multiple times. Google API控制台中的键会被多次检查。 Code was rewritten few times.... I really have no idea what could be wrong. 代码被重写了几次。。。我真的不知道有什么问题。

Thank you :) 谢谢 :)

You might have forgotten to configure Google API Console. 您可能忘记了配置Google API控制台。 Follow the instructions: https://developers.google.com/identity/sign-in/android/start-integrating 请按照说明进行操作: https : //developers.google.com/identity/sign-in/android/start-integrating

You see to create OAuth client ID for Android with corresponding package name and signing certificate's SHA1. 您将看到使用相应的包名称和签名证书的SHA1为Android创建OAuth客户端ID。 You do NOT have to enter this key anywhere in the code. 您不必在代码中的任何地方输入此密钥。 It just have to exist in Google API Console. 它只必须存在于Google API控制台中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM