Google Paly sign in Does not work in Android

2019-08-22 07:09发布

问题:

I am implementing google play sing in, but it does not work

I follow the developer document to implement it but still, it does not work

回答1:

Step1 : Set up the app in the Google Play Console

  1. Create a new app .
  2. Then, in your app-level build.gradle file, declare Google Play services as a dependency:

    compile 'com.google.android.gms:play-services-auth:16.0.1'

  3. Sign in to Google Play Console. . If you haven't registered for the Google Play Console before, it will prompt you to register. Add your app to Google play console

  4. Add your app to Google play console .

    a. Open the Game Services page, then click the Add New Game button on the left.

    b. Since you are creating from scratch, select the tab I don't use any Google APIs in my game yet . Enter your app name and select a category, and then click the Continue button.

    c. In the Detail form Only the display name and description are required for testing. The other fields must be filled out before you can publish your game.

    d. Click save .

    e. Open the page Liked apps on the right and click Android and enter the name of the app and the package name in the relevant field.

    f. Enable the appropriate multiplayer settings, real-time or turn- based.

    g. Select anti privacy on and Click save and continue.

    h. Now click the Authorize your app now button .

    i. In the Package name field, enter your package name and enter you app SHA1 in the Signing certificate fingerprint (SHA1) field . and Click Confirm.

    j. After you click the Confirm button, you should see your new client ID for this application. Copy and paste it in a safe place .

    k. Make sure that the account you intend to sign in with (the account on the test device) is listed as a tester in the project on your Developer Console setup (check the list in the "Testing" section)

Step 2: Make the following changers in your app

  1. In your manifest file under application tag add then following meta tag and replace “YOUR APPID” with the client Id. That you got when you authorize your application in The Google play console.
  2. To add standard Google sign-in button in your app, Include a com.google.android .gms.common .SignIn Button the main activity layout.

  3. Initiate your sing in Button in On Create method of the Activity.

    signInButton = findViewById(R.id.sign_in_button)

  4. When the user click on the sign in button . The following code snip sends the sign in intend .

    signInButton.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View view) {
          startSignInIntent();
          }
       });
    
  5. The following code snip shows you how to initialize the startsignin()
    Method

    private void startSignInIntent() {

    GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
    GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    Intent intent = signInClient.getSignInIntent();
    startActivityForResult(intent, RC_SIGN_IN);
       }
    
  6. In the onActivityResult() callback, handle the result from the returned
    Intent.

     **@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent 
      data) {
     super.onActivityResult(requestCode, resultCode, data);
     if (requestCode == RC_SIGN_IN) {
          GoogleSignInResult result =  
          Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
           GoogleSignInAccount signedInAccount = result.getSignInAccount();
       } else {
           String message = result.getStatus().getStatusMessage();
           if (message == null || message.isEmpty()) {
               message = getString(R.string.signin_other_error);
           }
           new AlertDialog.Builder(this).setMessage(message)
                   .setNeutralButton("Login Fail", null).show();
       }
    

    } }**