Android:Google登录-"DEFAULT_SIGN_IN"和"DEFAULT_GAMES_SIGN_IN"之间的区别 [英] Android: Google Sign In - difference between 'DEFAULT_SIGN_IN' and 'DEFAULT_GAMES_SIGN_IN'

查看:308
本文介绍了Android:Google登录-"DEFAULT_SIGN_IN"和"DEFAULT_GAMES_SIGN_IN"之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在AndroidStudio中的我的应用程序中实现Google登录.我可以和它一起工作:

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

我已通过API控制台将客户端ID正确设置为Web应用程序客户端ID,并且肯定已正确设置SHA1.

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.

当我更改为此(改为使用DEFAULT_GAMES_SIGN_IN)时:

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

我收到一条错误消息,消息为null,状态代码为12501.

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

DEFAULT_GAMES_SIGN_IN和DEFAULT_SIGN_IN有什么区别?我需要为游戏登录做些不同的事情吗?

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?

这是我所有的代码:

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

}

推荐答案

一个区别是DEFAULT_GAMES_SIGN_IN请求

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

这是在您的应用程序中使用Google Play游戏api所必需的.

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

(注意:我不能100%地确定它是精简版.它可能会请求Games.SCOPE_GAMES.但是,

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

但是,要登录Google Play游戏,必须使用Google Play游戏服务来设置您的应用.

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.

这篇关于Android:Google登录-"DEFAULT_SIGN_IN"和"DEFAULT_GAMES_SIGN_IN"之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆