简体   繁体   中英

Trouble Implementing Google Play Sign in with app in Android Studio

I have followed the directions on the google developer site, but can not get it right...

Here are my relevant code snippets from my MainActivity:

imports:

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;

implementations:

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

Variables:

    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;

This is instantiated 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 cycle methods:

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

Implemented methods:

    @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);
        }
    }
}

Note: I currently do not have a sign in button, just trying to have the user auto-prompted to sign in.

The app crashes a second after it pops up.

I think may be missing a meta tag or permission? Currently this is all I have.

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

and

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

The other possibility is an improper set up on the google developer console. What all do I need to do to get this thing to connect?! I am lost!! Thanks.

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)

Also, I read something about generating a client ID and downloading a JSON file and importing it into your project, maybe this could be the problem? I generated a client ID, but have not included any JSON file, how would I do that?

UPDATE

I changed to:

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

Removing these two lines that I originally had:

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

The app no longer crashes when it loads, but there is still no attempt to connect, or no google sign in window that pops up.

I see that you no longer have the errors you were complaining about, and the only thing missing is the step to connect. Try to add:

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();
}

to your onResume .

--EDIT--

I see that you get the error API_UNAVAILABLE . I think you haven't activated the Google+ API. Go to the official Google tutorial, Step 1 . Pay close attention to the step 4 and make sure you enable the Google+ API. You'll need to see something like in this picture.

在此处输入图片说明 .

Notice the "Disable API" button. That means the API is enabled for sure.

One more thing that might be the key to your problem here. Try to add this permissions to your manifest

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM