Android应用中的Google登录无法正常工作

时间:2016-12-12 14:16:20

标签: android android-studio google-signin googlesigninapi

我无法将我的应用与Google登录集成。 似乎从 handleSignInResult(GoogleSignInResult结果)返回的结果是错误的(应该是真的)。

有人可以帮助解释为什么会这样吗?我无法弄清楚会发生什么事情会导致它失败(包含和不包含令牌都会失败)。

public class GoogleSignIn extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{

//Pass this client ID to the requestIdToken or requestServerAuthCode method when you create the GoogleSignInOptions object.

private final int RC_SIGN_IN = 9001;
private final String TOKEN_CRED_ID = "*******-the web application credentials copied from https://console.developers.google.com/apis/credentials";
private GoogleApiClient mGoogleApiClient;
private SignInButton SignInBtn;
private TextView SignInStatusView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_sign_in);

    SignInBtn = (SignInButton) findViewById(R.id.sign_in_button);
    SetButtons();
    SignInStatusView = (TextView) findViewById(R.id.google_sign_in_status);
    // Get the user's ID and basic profile info using google

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(TOKEN_CRED_ID)
            .build();


    // This object is used in order to access the Google API for Signing in

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this/* AppCompatActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();


    SignInBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn();
        }
    });

    final Button SignOutBtn = (Button) findViewById(R.id.sign_out_and_disconnect);
    SignOutBtn.setVisibility(View.GONE);
    SignOutBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signOut();
        }
    });


}

private void CustomizeSignInButton()
{
    SignInBtn = (SignInButton) findViewById(R.id.sign_in_button);
    SignInBtn.setSize(SignInButton.SIZE_STANDARD);
}

private void SetButtons()
{
    CustomizeSignInButton();

    Button back = (Button) findViewById(R.id.go_back);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GoogleSignIn.this.finish();
        }
    });


}


protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}



protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, RC_SIGN_IN);
}

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



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("\n\n\n\nRequest code: " + requestCode);
    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        if (resultCode == RESULT_OK)
        {
            System.out.println("OK RESULT");
        }
        else
        {
            System.out.println("NOT OK RESULT");
        }
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInResult(result);
    }
}


private void handleSignInResult(GoogleSignInResult result) {
    System.out.println("handleSignInResult:" + result.isSuccess());
    if (result.isSuccess()) {
        // Signed in successfully, show authenticated UI.
        GoogleSignInAccount acct = result.getSignInAccount();
        SignInStatusView.setText(getString(R.string.signed_in, acct.getDisplayName()));
        updateUI(true);
        SaveClientDetails(acct);
    } else {
        // Signed out, show unauthenticated UI.
        updateUI(false);
    }
}


private void updateUI(boolean signedIn) {
    if (signedIn) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
    } else {
        SignInStatusView.setText(R.string.signed_out);
        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
    }
}


private void SaveClientDetails(GoogleSignInAccount account)
{
    // Save to DB
    System.out.println("*\nIn SAVE\n*");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    // Connection failed completely. Sign in and other functions are not available.
    System.out.println("ERROR ::: Problems with connection");
}

}

1 个答案:

答案 0 :(得分:2)

1。获取SHA1密钥Link

2。粘贴PAckage名称和SHA-1指纹,然后点击启用Google登录。然后最后点击生成配置文件以下载您的google-services.json From This Link

3. 将google-services.json文件移至应用文件夹Reffer Link

4. 现在运行