Google登录集成(Android)
测试google登录的条件:
1、手机具备*能力
2、手机为google手机或者安装有google服务
3、应用必须被正确的签名文件签名
参考google开发文档:
https://developers.google.com/identity/sign-in/android/sign-in
集成步骤:
第一步:注册登录google移动平台
第二步:添加你的超赞项目,设置地区
第三步:创建项目完成后,选择集成平台
第四步:选择Android,设置包名、sha1等信息
第五步:注册应用,然后跳转的页面中切记要下载json文件,并将json文件放到主module根目录下
第六步:
项目级 build.gradle(/build.gradle):
buildscript {
dependencies {
// Add this line
classpath ‘com.google.gms:google-services:3.2.0’
}
}
应用级 build.gradle:
dependencies {
// Add this line
compile ‘com.google.firebase:firebase-core:12.0.1’
}
…
// Add to the bottom of the file
apply plugin: ‘com.google.gms.google-services’
第七步:环境配置完成,Sync Now…
第八步:sdk 需要下载google_play_services
第九步:定义初始化回调接口,定义登录回调接口:
public interface GoogleInitListener {
void initFinish(GoogleSignInAccount account,GoogleSignInClient client );
}
public interface GoogleLoginListener {
void loginSuccess(GoogleSignInAccount account);
void LoginFailed(String msg);
}
第十步:点击google登录按钮开始进行初始化:
String google_app_id = "your app id";
public void gooleAuthInit( Activity activity,GoogleInitListener googleInitListener){
GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(googleId)
.requestEmail()
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(activity, gso);
//如果用戶已经登录返回account对象,没有登录 返回null
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(activity);
if(account==null){
//初始化完成,检查到google账户没有登录
googleInitListener.initFinish(null,mGoogleSignInClient);
}else{
//初始化完成,检查到google账户已经登录,返回账号信息
googleInitListener.initFinish(account,mGoogleSignInClient);
}
}
第十一步:收到初始化回调对象时,调用登录:
private GoogleLoginListener mGoogleAuthListener;
//打开google登录页
public void gooleAuthLogin(Activity activity, GoogleSignInClient mGoogleSignInClient,GoogleLoginListener googleAuthListener){
mGoogleAuthListener = googleAuthListener;
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
activity.startActivityForResult(signInIntent,RC_SIGN_IN);
}
//登录结果返回
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
// The Task returned from this call is always completed, no need to attach
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.
mGoogleAuthListener.loginSuccess(account);
} 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());
Log.d(TAG, e.getMessage());
e.printStackTrace();
mGoogleAuthListener.LoginFailed(e.getStatusCode()+","+e.getMessage());
}
}
登录流程到此结束,欢迎大家留言批评指正,谢谢!
上一篇: php画图函数画不出图来,
推荐阅读
-
Google登录集成(Android)
-
Xinstall集成Android和iOS以及Web三端快速集成SDK与使用
-
php-android APP 实现微信登录,并同PHP服务器建立连接通信 (悬赏10C币)
-
小弟我做的一个网站想集成facebook帐号登录,如何弄啊
-
Swift项目集成QQ授权登录
-
将 Google Book Search 集成到 PHP 应用程序中_PHP
-
Android webview 设置cookie 保持登录状态
-
php脚本生成google play url的下载链接,下载apk并自动反编译后获取android版本号
-
android通过google api获取天气信息示例
-
小弟我做的一个网站想集成facebook帐号登录,如何弄啊