Android 集成 google 登录并获取性别等隐私信息的实现代码
前言
公司做海外产品的,集成的是 google 账号登录,账号信息、邮箱等这些不涉及隐私的按 google 的正常登录流程可以轻松实现 。但是一旦需要获取涉及隐私的信息就比较麻烦,文档也不是十分清晰,非常难找,很多坑。
google 账号登录
官方链接:https://developers.google.com/identity/sign-in/android/start
https://developers.google.com/identity/sign-in/android/sign-in
google 账号登录接入的坑:
- 申请的
client_id
必须是 api console 后台 :https://console.cloud.google.com/apis 与 google play 后台对应的应用关联起来。 -
client_id
下的签名信息和报名信息必须和测试时的 apk 的签名信息和报名信息一致。 - 在 google play 下启动 google 的二次签名,则 api console 后台的签名信息是二次签名后的信息。打包测试时使用上传 到 google play 后台的 apk 的签名证书即可。
google 登录的流程在这个文档写的比较清楚了:https://developers.google.com/identity/sign-in/android/sign-in,这里大致说一下,不贴代码了
构建需求请求的内容:
googlesigninoptions gso = new googlesigninoptions.builder(googlesigninoptions.default_sign_in) .requestemail() .requestidtoken("your client_id") .build(); // build a googlesigninclient with the options specified by gso. mgooglesigninclient = googlesignin.getclient(this, gso);
2.发起登录请求,跳转 google 登录页面。
intent signinintent = mgooglesigninclient.getsigninintent(); startactivityforresult(signinintent, rc_sign_in);
获取 google 登录返回
@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 == rc_sign_in) { // the task returned from this call is always completed, no need to attach // a listener. task<googlesigninaccount> task = googlesignin.getsignedinaccountfromintent(data); handlesigninresult(task); } }
获取 用户 id token,传到你自己的 服务端 做验证
private void handlesigninresult(task<googlesigninaccount> completedtask) { try { googlesigninaccount account = completedtask.getresult(apiexception.class); // signed in successfully, show authenticated ui. } 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()); } }
切换账号
/** * 重新获取账号列表 */ public void revokeaccess() { try { if (mgooglesigninclient!=null && mactivity!=null){ mgooglesigninclient.revokeaccess().addoncompletelistener(mactivity, new oncompletelistener<void>() { @override public void oncomplete(@nonnull task<void> task) { log.d(tag, "oncomplete: "); } }); } } catch (exception e){ e.printstacktrace(); } }
获取公开资料和需要特别授权的信息(性别、生日等)
1、在构建请求是新增获取 的公共资料信息 及 需要获取的特殊信息
private static final string gender_scope = "https://www.googleapis.com/auth/user.gender.read"; googlesigninoptions gso = new googlesigninoptions.builder(googlesigninoptions.default_sign_in) .requestemail() .requestidtoken("your client_id") .requestscopes(new scope(gender_scope)); .build(); // build a googlesigninclient with the options specified by gso. mgooglesigninclient = googlesignin.getclient(this, gso);
需要请求的信息可在如下链接查找:
2、检测是否有权限
googlesigninaccount lastsignedinaccount = googlesignin.getlastsignedinaccount(mactivity); scope scope = new scope(gender_scope); if (utils.isneedrequest() && !googlesignin.haspermissions(lastsignedinaccount,scope)){ sglog.d(tag+" need requst permission..."); googlesignin.requestpermissions(mactivity,rc_get_token,lastsignedinaccount,scope); }
注意:这一步不需要也可以,有这一步会出现一个 “再确认” 的授权页面,没有也不影响获取的信息。
3、跳转登录页面 (同以上 google 账号登录)
4、获取登录信息 (同以上 google账号登录)
5、开启线程获取 特殊信息
getprofileasynctask = new getprofileasynctask(mactivity, new gpprofileinfocallback() { @override public void ongetprofileinfo(person person) { sglog.d(tag+" ongetprofileinfo... "); getprofileinfo(person); } }); getprofileasynctask.execute(signinaccount);
异步任务
// global instance of the http transport private static final httptransport http_transport = androidhttp.newcompatibletransport(); // global instance of the json factory private static final jsonfactory json_factory = jacksonfactory.getdefaultinstance(); private static class getprofileasynctask extends asynctask<googlesigninaccount, void, person> { // retrieved from the sigin result of an authorized googlesignin private weakreference<activity> mactivityref; private gpprofileinfocallback mprofileinfocallback; public getprofileasynctask(activity activity,gpprofileinfocallback callback) { mactivityref = new weakreference<>(activity); mprofileinfocallback = callback; } @override protected person doinbackground(googlesigninaccount... params) { if (mactivityref.get() == null){ sglog.d(tag+" getprofileasynctask doinbackground activity is null."); return null; } googlesigninaccount signinaccount = params[0]; context context = mactivityref.get().getapplicationcontext(); googleaccountcredential credential = googleaccountcredential.usingoauth2( context, collections.singleton(gender_scope)); credential.setselectedaccount(signinaccount.getaccount()); sglog.d(tag+" get profile info start."); peopleservice service = new peopleservice.builder(http_transport, json_factory, credential) .setapplicationname(apkutils.getappname(context)) // your app name .build(); sglog.d(tag+" get profile info start."); // get info. on user person person =null; try { person = service .people() .get("people/me") .setpersonfields("genders") .execute(); sglog.d(tag+" getperson end."); // return the result if (mprofileinfocallback!=null){ mprofileinfocallback.ongetprofileinfo(person); } } catch (exception e) { sglog.e(tag+e.getmessage()); if (mprofileinfocallback!=null){ mprofileinfocallback.ongetprofileinfo(null); } e.printstacktrace(); } return person; } @override protected void onpostexecute(person avoid) { super.onpostexecute(avoid); } }
获取性别信息
private void getprofileinfo(person person){ sglog.d(tag+" executeprofileinfo..."); if (person == null){ notifyresult(mlastuser,utils.success); }else { try { list<gender> genders = person.getgenders(); gender gender = genders.get(0); string value = gender.getvalue(); sglog.d(tag+" genders:"+genders.size()+ " gender:"+value); mlastuser.setgender(value); notifyresult(mlastuser,utils.success); }catch (exception e){ sglog.e(tag+" getprofileinfo error."); notifyresult(null,sgerrorcode.login_failed); e.printstacktrace(); } } }
参考文献:
总结
到此这篇关于android 集成 google 登录并获取 性别等隐私信息的文章就介绍到这了,更多相关android 集成 google 登录并获取 性别等隐私信息内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!