请求服务器身份验证代码时,GoogleSignInResult 在 Android 应用程序中返回 DEVELOPER_ERROR

Posted

技术标签:

【中文标题】请求服务器身份验证代码时,GoogleSignInResult 在 Android 应用程序中返回 DEVELOPER_ERROR【英文标题】:GoogleSignInResult returns DEVELOPER_ERROR in Android app when requesting server auth code 【发布时间】:2017-12-05 19:30:49 【问题描述】:

我正在按照本手册将 Google People API 连接到 android 应用:http://blog.iamsuleiman.com/people-api-android-tutorial-1/

我正在使用以下代码登录:

GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestEmail()
        .requestScopes(new Scope(Scopes.PLUS_LOGIN),
                new Scope(PeopleScopes.CONTACTS_READONLY),
                new Scope(PeopleScopes.USER_PHONENUMBERS_READ))
        .requestServerAuthCode(getString(R.string.google_oauth_client_id), false)
        .build();

    mGoogleApiClient = new GoogleApiClient.Builder(getContext())
            .enableAutoManage(getActivity(), this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions)
            .build();

    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent( mGoogleApiClient );
    startActivityForResult( signInIntent, GOOGLE_PLUS_RC_SIGN_IN );

onActivityResult 代码为:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);

    GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
    ...

我不断收到 DEVELOPER_ERROR 结果。

该应用由我在开发者控制台中设置的 SHA1 指纹代码签名。

OAUTH 客户端 ID 取自我的应用的“Web 客户端”JSON 配置。

所有 API 都在 Google 开发者控制台中启用

如果我删除方法 .requestServerAuthCode():

    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(Scopes.PLUS_LOGIN),
                    new Scope(PeopleScopes.CONTACTS_READONLY),
                    new Scope(PeopleScopes.USER_PHONENUMBERS_READ))
            .build();

结果

Auth.GoogleSignInApi.getSignInResultFromIntent(data);

成功:应用正在请求权限。

我做错了什么?

为什么 requestServerAuthCode 会导致 DEVELOPER_ERROR,尽管在 Google 的手册中有使用此方法的示例:

https://developers.google.com/identity/sign-in/android/offline-access#enable_server-side_api_access_for_your_app

是否有任何示例如何在 Android 应用中使用 People API?

【问题讨论】:

我遇到了同样的问题。你知道如何解决这个问题吗? 【参考方案1】:

谷歌登录:-

     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();

GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso); 

在你的 btn 点击调用这个方法:-

private void signIn() 
        progressDialog(this, "Please wait...", true);
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    

onActivityResult:-

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
        super.onActivityResult(requestCode, resultCode, data);
        progressDialog(this, "Please wait...", false);
        if (requestCode == RC_SIGN_IN) 

            try 
                // Google Sign In was successful, authenticate with Firebase
                Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
                handleSignInResult(task);
             catch (Exception e) 
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
                // ...
            
        
    

最后保存你的细节:-

private void handleSignInResult(Task<GoogleSignInAccount> task) 
        try 
            GoogleSignInAccount account = task.getResult(ApiException.class);
            socialId = account.getId();
            socialName = account.getDisplayName();
            socialEmail = account.getEmail();
            //you have got all the details now you can go your next screen
         catch (ApiException e) 
            Toast.makeText(this, "Authentication failed", Toast.LENGTH_SHORT).show();
        
        mGoogleSignInClient.signOut();
     

【讨论】:

【参考方案2】:

而不是将您的生产/调试 client_id 传递到

.requestServerAuthCode()

方法改用 Web client_id - 我认为 google 登录将始终使用 string.xml 中的 server_client_id

所以从技术上讲,您需要同时使用这两个键。

【讨论】:

以上是关于请求服务器身份验证代码时,GoogleSignInResult 在 Android 应用程序中返回 DEVELOPER_ERROR的主要内容,如果未能解决你的问题,请参考以下文章

谷歌使用 GIDSignin 多次登录

使用 Firebase 身份验证和谷歌登录时注销时“GoogleApiClient 尚未连接”

如何在身份服务器 4/身份验证代码流中请求访问令牌的附加声明?

使用JAVA请求需要Basic身份验证的网页

身份验证后请求其他服务时如何从 JWT 令牌获取用户名?

通过 Ajax 请求时完成 OpenIDConnect 身份验证