谷歌登录开发者平台注册地址为https://console.firebase.google.com/,并不是在https://console.developers.google.com/上进行注册,一开始我也是参考网上的帖子 在谷歌的developers网站上进行注册, 流但发现流程一直走不通,最后发现需要在firebase网站上进行注册,具体流程直接上图,简单粗暴明了:
通过上述6步就能获取到一个google-service.json文件,把它拷贝的项目的根目录下,如图6,点图6的CONTINUE按钮,会有直接示例代码。
主module的guild.gradle中的依赖,注意保证三个依赖的版本一致性:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.firebase:firebase-core:11.2.0'
compile 'com.google.firebase:firebase-auth:11.2.0'
compile 'com.google.android.gms:play-services-auth:11.2.0'
}
根目录下的build.gradle:
sha1的获取方式就不在这复述了,网上有很多。最后,需要注意两个事:
1.注册网址需要翻墙;
2.测试手机需要翻墙且支持google service。
下面是我的简单测试代码,需要的话可以参考一下:
public class MainActivity extends FragmentActivity implements View.OnClickListener {
private static final String LOG_TAG = "GAccountDemo";
private static final int RC_SIGN_IN = 0x100;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SignInButton signInButton = findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setOnClickListener(this);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestId()
.requestProfile()
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(MainActivity.this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent, RC_SIGN_IN); //RC_SIGN_IN是requestcode
break;
}
}
@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);
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
Log.i(LOG_TAG, "id--------" + account.getId() + "----name----" + account.getDisplayName() + "---photo--" + account.getPhotoUrl());
}
}
}