Android: Google Sign In - difference between 'DEFAULT_SIGN_IN' and 'DEFAULT_GAMES_SIGN_IN'

℡╲_俬逩灬. 提交于 2020-12-06 06:54:16

问题


I am trying to implement a Google Sign In into my app in AndroidStudio. I have it working with:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().requestServerAuthCode(clientID).build();

I have my client ID correctly set up to the Web Application Client ID from the API console and SHA1 is definitely set up correctly.

When i change to this (using DEFAULT_GAMES_SIGN_IN instead):

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).requestEmail().requestServerAuthCode(clientID).build();

I get an error with message as null and status code as 12501.

What is the difference between DEFAULT_GAMES_SIGN_IN and DEFAULT_SIGN_IN? Do i need to do something different for the games sign in?

Here is all my code:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

public class MainActivity extends AppCompatActivity {

GoogleSignInClient mGoogleSignInClient;
String clientID;
int RC_SIGN_IN = 9001;

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

    clientID = getString(R.string.client_id);

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).requestEmail().requestServerAuthCode(clientID).build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    signInSilently();

}

private void signInSilently(){
    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this, new OnCompleteListener<GoogleSignInAccount>() {
        @Override
        public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
            if(task.isSuccessful()){
                GoogleSignInAccount signedInAccount = task.getResult();
                setText(signedInAccount.getEmail(), signedInAccount.getDisplayName());
            }else{
                interactiveSignIn();
            }
        }
    });
}

private void interactiveSignIn(){
    Intent i = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(i, RC_SIGN_IN);
}

@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();
            setText(signedInAccount.getEmail(), signedInAccount.getDisplayName());
        }else{
            String message = "ERROR: " + result.getStatus().getStatusMessage() + " - CODE: " + result.getStatus().getStatusCode();
            showAlert(message);
        }
    }
}

void showAlert(String message){
    new AlertDialog.Builder(this).setMessage(message).setNeutralButton("OK", null).show();
}

void setText(String email, String name){
    TextView tv = (TextView) findViewById(R.id.Test_TextView);

    tv.setText("EMAIL: " + email + "   NAME: " + name);
}

}


回答1:


One difference is that DEFAULT_GAMES_SIGN_IN requests the Games.SCOPE_GAMES_LITE scope.

This is required to use Google Play Games apis within your application.

(Note: I am not 100% certain that it is the lite scope. It may request Games.SCOPE_GAMES instead. However, a search on SCOPE_GAMES in the documentation brings up deprecated interfaces whose new alternatives all require SCOPE_GAMES_LITE)

However, in order to sign in to Google Play Games, your app must be set up with Google Play Games Services.

Refer to the official guide on how to do this.

Follow the instructions carefully the first time, as any little mistake will leave you with an unsuccessful sign-in.

After the set up, you may want to look at the Games-specific sign-in guide.



来源:https://stackoverflow.com/questions/49017777/android-google-sign-in-difference-between-default-sign-in-and-default-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!