Google三方登录流程
有更新,请直接查看Google三方登录流程及注意事项.pdf,文章可忽略(免费下载)
普通的登录只能获取到用户的邮箱,全名,头像的URL,如需要获取其他更多数据,请查看Google登录获取用户的更多数据.zip(免费下载)
准备
- 注册一个google账号(方法自行百度)
步骤
打开GoogleAPIs,登录,看到如下界面
打开Google登录Android文档官方文档
-
添加Google Play服务
- 在项目的顶级
build.gradle
文件中,确保包含Google的Maven存储库:
allprojects { repositories { google() // If you're using a version of Gradle lower than 4.1, you must instead use: // maven { // url 'https://maven.google.com' // } } }
- 然后,在您的应用程序等级的
build.gradle
文件中,将Google Play服务声明为依赖项:
implementation 'com.google.android.gms:play-services-auth:19.0.0'
- 在项目的顶级
-
文档中有个配置项目,点击按照步骤配置就行(没有项目就新建,有的话直接选择就行)
这里根据需要选择,这里我就选择了Android
这里填入包名和SHA-1
ps:获取SHA-1除了他提示的方法外还要一种方法。直接在AS中查看
填写了之后会提示两个字符串,没截图忘了是什么了,可以复制下来,也有一个按钮可以下载下来,然后就可以在GoogleAPIs中查看
- 然后就是敲代码实现登录功能,直接上代码了,想要更详细了解进入官方文档查看
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private GoogleSignInClient mGoogleSignInClient;
private static final String TAG = "MainActivity";
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == 1) {
// The Task returned from this call is always completed, no need to attach
// a listener.
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
Log.d(TAG, "handleSignInResult: " + account.getEmail());
Log.d(TAG, "handleSignInResult: " + account.getId());
Log.d(TAG, "handleSignInResult: " + account.getPhotoUrl());
Log.d(TAG, "handleSignInResult: " + account.getIdToken());
Log.d(TAG, "handleSignInResult: " + account.getGrantedScopes());
Log.d(TAG, "handleSignInResult: " + account.getServerAuthCode());
} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
// updateUI(null);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//配置Google登录和GoogleSignInClient对象
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestId()
.requestIdToken(this.getString(R.string.google_server_client_id))
.requestProfile()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//Google登录按钮
findViewById(R.id.sign_in_button).setOnClickListener(this);
//自定义退出登录按钮
findViewById(R.id.btn_1).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
//检查现有的登录用户
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
Log.d(TAG, "onStart: " + (account == null ? true : false));
//检索登录用户的个人资料信息
if (account != null) {
String personName = account.getDisplayName();
String personGivenName = account.getGivenName();
String personFamilyName = account.getFamilyName();
String personEmail = account.getEmail();
String personId = account.getId();
Uri personPhoto = account.getPhotoUrl();
Log.d(TAG, "onStart: ---- personName is : " + personName +
"\n personGivenName is : " + personGivenName +
"\n personFamilyName is : " + personFamilyName +
"\n personEmail is : " + personEmail +
"\n personId is : " + personId +
"\n personPhoto is : " + personPhoto);
signOut();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
case R.id.btn_1:
signOut();
break;
}
}
//登录
private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Log.d(TAG, "signOut: -----------------------------");
// ...
}
});
}
//注销
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 1);
}
//断开帐户
//强烈建议您为使用Google登录的用户提供断开其Google帐户与您的应用的连接的功能。
// 如果用户删除其帐户,则必须删除您的应用程序从Google API获取的信息。
private void revokeAccess() {
mGoogleSignInClient.revokeAccess()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}
}
ps:在这里如果登录了账户,那么在点登录屏幕就会闪一下。
这里的google_server_client_id用的这个,Android那个我试了好像不行,不知道为什么
.requestIdToken(this.getString(R.string.google_server_client_id))这个不加的话有些信息就获取不到比如account.getPhotoUrl(),具体原因不知道,实际操作得出的结果
xml文件
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_1"
android:text="sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
最后记得加入网络权限
<uses-permission android:name="android.permission.INTERNET"/>
一个简单的登录,注销功能就这样了,官方文档上还有其他详细的内容,想了解更多请移步官方文档