在 Android Studio 中实施 Google 使用应用程序登录时遇到问题

Trouble Implementing Google Play Sign in with app in Android Studio

我已按照 google 开发者网站上的说明进行操作,但无法正确...

这是我的 MainActivity 中的相关代码片段:

进口:

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.games.Games;
import com.google.android.gms.plus.Plus;
import com.google.example.games.basegameutils.BaseGameUtils;

实现:

public class MainActivity extends Activity implements View.OnTouchListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener{ ...}

变量:

    GoogleApiClient mGoogleApiClient;

private static int RC_SIGN_IN = 9001;

private boolean mResolvingConnectionFailure = false;
private boolean mAutoStartSignInflow = true;
private boolean mSignInClicked = false;



SharedPreferences stats;
SharedPreferences achievements;
SharedPreferences.Editor stat_editor;
SharedPreferences.Editor achievement_editor;

这是实例化的onCreate()

        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    .addScope(new Scope(Scopes.PROFILE))
                    // add other APIs and scopes here as needed
            .addApi(Plus.API)
            .build();

Activity循环方式:

    @Override
protected void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

实现的方法:

    @Override
public void onConnected(Bundle bundle) {
    //hide sign in  button
}

@Override
public void onConnectionSuspended(int i) {
    // Attempt to reconnect
    mGoogleApiClient.connect();
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if (mResolvingConnectionFailure) {
        // already resolving
        return;
    }

    // if the sign-in button was clicked or if auto sign-in is enabled,
    // launch the sign-in flow
    if (mSignInClicked || mAutoStartSignInflow) {
        mAutoStartSignInflow = false;
        mSignInClicked = false;
        mResolvingConnectionFailure = true;

        // Attempt to resolve the connection failure using BaseGameUtils.
        // The R.string.signin_other_error value should reference a generic
        // error string in your strings.xml file, such as "There was
        // an issue with sign-in, please try again later."
        if (!BaseGameUtils.resolveConnectionFailure(this,
                mGoogleApiClient, connectionResult,
                RC_SIGN_IN, getString(R.string.signin_other_error))) {
            mResolvingConnectionFailure = false;
        }
    }

    // Put code here to display the sign-in button
}

protected void onActivityResult(int requestCode, int resultCode,
                                Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        mSignInClicked = false;
        mResolvingConnectionFailure = false;
        if (resultCode == RESULT_OK) {
            mGoogleApiClient.connect();
        } else {
            // Bring up an error dialog to alert the user that sign-in
            // failed. The R.string.signin_failure should reference an error
            // string in your strings.xml file that tells the user they
            // could not be signed in, such as "Unable to sign in."
            BaseGameUtils.showActivityResultError(this,
                    requestCode, resultCode, R.string.sign_in_failed);
        }
    }
}

注意:我目前没有登录按钮,只是想让用户自动提示登录。

应用程序在弹出后一秒崩溃。

我认为可能缺少元标记或权限?目前我只有这些。

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

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

另一种可能是 google 开发者控制台设置不当。我需要做什么才能让这个东西连接起来?!我搞不清楚了!!谢谢。

08-06 17:17:23.910  30903-30939/? E/HTTPMetricsTransport﹕ transmit - MissingCredentialsException while transmitting;
amazon.communication.MissingCredentialsException: Static Credential is unavailable.
        at com.amazon.client.metrics.transport.StaticCredentialRequestSigner.signRequest(StaticCredentialRequestSigner.java:44)
        at com.amazon.client.metrics.transport.MetricsHttpRequestSigner.signRequest(MetricsHttpRequestSigner.java:54)
        at amazon.communication.srr.HttpClientSrrManager.makeRequestSync(HttpClientSrrManager.java:190)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.makeRequest(HTTPMetricsTransport.java:286)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.attemptToTransmit(HTTPMetricsTransport.java:230)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.attemptToTransmit(HTTPMetricsTransport.java:235)
        at com.amazon.client.metrics.transport.HTTPMetricsTransport.transmit(HTTPMetricsTransport.java:202)
        at com.amazon.client.metrics.batch.transmitter.BatchTransmitter$QueuePusher.sendBatches(BatchTransmitter.java:161)
        at com.amazon.client.metrics.batch.transmitter.BatchTransmitter$QueuePusher.run(BatchTransmitter.java:127)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

此外,我阅读了一些有关生成客户端 ID 并下载 JSON 文件并将其导入您的项目的内容,也许这可能是问题所在?我生成了一个客户端 ID,但没有包含任何 JSON 文件,我该怎么做?

更新

我改为:

            mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
                    // add other APIs and scopes here as needed
            .addApi(Plus.API)
            .build();

删除我原来的这两行:

                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    .addScope(new Scope(Scopes.PROFILE))

应用程序在加载时不再崩溃,但仍然没有尝试连接,或者没有弹出 google 登录 window。

我看到你不再有你抱怨的错误,唯一缺少的是连接步骤。尝试添加:

if (mGoogleApiClient.isConnected()) {
    Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
    mGoogleApiClient.disconnect();
    mGoogleApiClient.connect();
} else if (!mGoogleApiClient.isConnecting()) {
    mSignInClicked = true;
    // uncomment the next line if you have this method.
    // resolveSignInError();
}

给你的 onResume.

--编辑--

我看到你遇到了错误 API_UNAVAILABLE。我想你还没有激活 Google+ API。转到 the official Google tutorial, Step 1。请密切注意第 4 步,确保启用 Google+ API。您需要在这张照片中看到类似的内容。

.

注意 "Disable API" 按钮。这意味着 API 肯定已启用。

还有一件事可能是您的问题的关键。尝试将此权限添加到您的清单

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