6

我按照本手册将 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?

4

2 回答 2

0

谷歌登录:-

     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();
    } 
于 2019-04-11T10:58:28.600 回答
0

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

.requestServerAuthCode()

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

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

于 2019-04-11T10:50:37.167 回答