android - 无法集成谷歌登录?

标签 android android-studio google-signin

我一直在尝试将 google 登录集成到我的 android 应用程序中,但每次都给出错误的结果。 我已经包含了 json 配置文件,也检查了 gradle 文件。 请帮忙,我被困住了。 这是我的代码

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signInButton.setScopes(gso.getScopeArray());
}



@Override
public void onStart() {
    super.onStart();

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
    if (opr.isDone()) {
        Log.d(TAG, "Got cached sign-in");
        GoogleSignInResult result = opr.get();
        handleSignInResult(result);
    } else {
        showProgressDialog();
        opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
            @Override
            public void onResult(GoogleSignInResult googleSignInResult) {
                hideProgressDialog();
                handleSignInResult(googleSignInResult);
            }
        });
    }
}


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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        int statusCode = result.getStatus().getStatusCode();
        Log.d(statusCode+"oo", "joij");
        handleSignInResult(result);
    }
}

private void handleSignInResult(GoogleSignInResult result) {
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    Log.d(result+"hello", "hii me prateek");
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        mStatusTextView.setText("done");
        updateUI(true);
    } else {
        mStatusTextView.setText("not done");
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}
private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

private void revokeAccess() {
    Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(
            new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    // [START_EXCLUDE]
                    updateUI(false);
                    // [END_EXCLUDE]
                }
            });
}

最佳答案

您只是忘记请求 scope 和 idToken,见下文。

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
            .requestIdToken(getString(R.string.server_client_id))
            .requestEmail()
            .build();

关于android - 无法集成谷歌登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39225713/

相关文章:

Android 6.0 Marshmallow BLE 连接问题

intellij-idea - 如何在 Intellij IDEA 中删除 xml 标签的灰色突出显示?

android-studio - Android Studio 1.0.2消息在“查看”菜单下显示为灰色

android - Flutter:方法 'signInWithGoogle' 没有为类 'FirebaseAuth' 定义

javascript - Expo Google 登录 DEVELOPER_ERROR

google-api - Google登录API : isSignedIn. get()返回不一致的值

android - 跟踪 Android 上的电量使用情况

android - 如何安排服务在android中运行

android - 安装跟踪是否需要导出?

java - HttpRequestException : Could Not Head warning in Android Studio 3. 1.1(但同步成功)