安卓Google+登录无法使用 - 状态码=需要登录

3
我正在尝试使用Google+让用户登录。我已经在Google开发人员控制台创建了项目,获取了客户端ID,并在Android Studio中安装了Google Play服务。(我的运行设备是Android 4.4.2)当应用程序运行时,我按下登录按钮后会出现一个提示消息:“内部错误”。Logcat中的错误如下:
error ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{429ffe38: android.os.BinderProxy@429ffdd8}}

请注意,这个错误是在用户按下按钮之前出现的。事实上,通过调试,我发现它来自于这行代码:

Log.e(TAG, "error " + result.toString());

这段文字出现在方法中:

public void onConnectionFailed(ConnectionResult result) {
    if (!mGoogleIntentInProgress) {
        /* Store the ConnectionResult so that we can use it later when the user clicks on the Google+ login button */
        mGoogleConnectionResult = result;

        if (mGoogleLoginClicked) {
            /* The user has already clicked login so we attempt to resolve all errors until the user is signed in,
             * or they cancel. */
            resolveSignInError();
        } else {
            Log.e(TAG, "error " + result.toString());
        }
    }
}

以下是其他重要的方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    SignInButton googleLoginButton = (SignInButton) findViewById(R.id.login_with_google);
    googleLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mGoogleLoginClicked = true;
            if (!mGoogleApiClient.isConnecting()) {
                if (mGoogleConnectionResult != null) {
                    resolveSignInError();
                } else if (mGoogleApiClient.isConnected()) {
                    getGoogleOAuthTokenAndLogin();
                } else {
                /* connect API now */
                    Log.d(TAG, "Trying to connect to Google API");
                    mGoogleApiClient.connect();
                }
            }
        }

    });

    /* Setup the Google API object to allow Google+ logins */
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

 private void resolveSignInError() {
    if (mGoogleConnectionResult.hasResolution()) {
        try {
            mGoogleIntentInProgress = true;
            mGoogleConnectionResult.startResolutionForResult(this, RC_GOOGLE_LOGIN);
        } catch (IntentSender.SendIntentException e) {
            // The intent was canceled before it was sent.  Return to the default
            // state and attempt to connect to get an updated ConnectionResult.
            mGoogleIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_GOOGLE_LOGIN) {
        /* This was a request by the Google API */
        if (resultCode != RESULT_OK) {
            mGoogleLoginClicked = false;
        }
        mGoogleIntentInProgress = false;
        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }


}

以下是AndroidManifest.xml文件中的代码:

<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />

还有元数据

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Hope you can help me, thanks a lot!


我相信谷歌目前登录存在问题。他们的OAuth登录提供商一直返回access_denied。这让我疯狂了30分钟 - 快速查看Twitter,似乎这是谷歌端的问题。 - Tom
2个回答

6

在我的项目中,使用Google+登录经常出现一些问题。例如,在创建mGoogleApiClient时,您应该:

           mGoogleApiClient = new GoogleApiClient.Builder(this) 
                .addApi(Plus.API) 
                .addScope(Plus.SCOPE_PLUS_LOGIN) 
                .addScope(Plus.SCOPE_PLUS_PROFILE) 
                .addConnectionCallbacks(this) 
                .addOnConnectionFailedListener(this) 
                .build();

使用 SCOPE_PLUS_PROFILE - OAuth 2.0 权限以访问用户的 Google+ 个人资料。

你需要启用 Google+ API 并创建带有 SHA1 指纹和你的包名的凭证。

在 OAuth 同意屏幕上输入你的电子邮件产品名称(必填)。



此外,请确保以下内容相同:

在 <application> 标签中的applicationId
AndroidManifest.xml 中的包名
Google Developer Console 凭证中的包名

都要相同


此外,请确保你拥有以下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

在方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data { ... ... }

不需要调用 super.onActivityResult(requestCode, resultCode, data);

还有一些重要的事情: 调试密钥库发布密钥库

还有更多……


0
根据您的操作,如果您不需要保持登录状态,可以保持连接处于打开状态。
替换
mGoogleApiClient.connect();

使用

mGoogleApiClient.connect(2); // SIGN_IN_MODE_OPTIONAL = 2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接