【问题标题】:Request is missing required authentication credential. Expected OAuth 2 access token请求缺少所需的身份验证凭据。预期的 OAuth 2 访问令牌
【发布时间】:2020-05-29 17:07:04
【问题描述】:

我们正在尝试将 Google Sign 选项添加到我们的项目中。我们已成功将此选项添加到站点和 ios 应用程序。但是在添加到android应用程序时遇到了困难。使用此页面中的“配置项目”选项https://developers.google.com/identity/sign-in/android/start-integrating 我指定了 Andrid 调试密钥 (Gradle -> android -> signingReport) 的 SHA1 和包名称 (com.my.app)。此表单为我们提供了 WEB 类型的客户端 ID。我下载了credentials.json并把它放在项目的app文件夹中。我还在 Google API 控制台中找到了一个带有指定 SHA1 哈希的 android 类型客户端 ID,但我不明白在哪里使用它(如果我们使用 android 类型的 oauth 客户端,我们会收到错误“signInResult:failed code=10”,所以我们应该使用 web 类型的客户端,并且在 API 控制台中没有这样的 android 客户端,我们会得到 12500 错误并且没有令牌。

接下来我们根据文档https://developers.google.com/identity/sign-in/android/backend-auth添加了一个示例代码来获取令牌并将其传递给我们的后端服务器。

private void enterGoogle() {
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestIdToken("12345-OurWebTypeClient.apps.googleusercontent.com").build();
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            String accessToken = account.getIdToken();
            authWithGoogleToken(accessToken);

        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());

        }
    }

    private void authWithGoogleToken(String accessToken) {
        if (accessToken == null)
            Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
        else
            RetrofitFactory.getInstance().authGoogle(accessToken)
                .enqueue(new retrofit2.Callback<JsonObject>() {
                    @Override
                    public void onResponse(retrofit2.Call<JsonObject> call, retrofit2.Response<JsonObject> response) {
                        try {
                            if (response.isSuccessful() && response.body() != null) {
                                if (!response.body().has("error")) {

                                    Toast.makeText(MainActivity.this, "Success!", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(MainActivity.this, response.body().get("error").getAsString(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(retrofit2.Call<JsonObject> call, Throwable t) {
                        Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
                    }
                });
    }

使用此代码,我们得到从“eyJ”开始的长标记。如果我们稍微更改代码以使用替代方法:

.requestServerAuthCode("12345-OurWebTypeClient.apps.googleusercontent.com").build();

String accessToken = completedTask.getResult().getServerAuthCode();

我们得到从“4/0”开始的短令牌。我们不知道这两种类型的令牌与两种获取令牌的方法有什么区别,但是在后端服务器上,两种类型的令牌都有一个错误:

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

例如,对于来自 iOS SDK 的令牌,我们没有此类错误。请帮助找出在 Android 应用中实施 Google 登录有什么问题?

【问题讨论】:

    标签: android google-signin


    【解决方案1】:

    我们使用此解决方法解决了它

    GoogleSignInAccount account = completedTask.getResult(ApiException.class);
                AsyncTask.execute(() -> {
                    try {
                        String accessToken = GoogleAuthUtil.getToken(context, account.getAccount(), "oauth2:" + Scopes.PLUS_LOGIN);
                        runOnUiThread(() -> {
                            authWithGoogleToken(accessToken);
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    
                String accessToken = account.getIdToken();
                authWithGoogleToken(accessToken);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 2021-03-02
      • 2020-09-11
      • 2019-06-23
      • 1970-01-01
      • 2018-07-21
      相关资源
      最近更新 更多