实现QQ、微信、新浪微博和百度第三方登录(Android Studio)
前言:
对于大多数的APP都有第三方登录这个功能,自己也做过几次,最近又有一个新项目用到了第三方登录,所以特意总结了一下关于第三方登录的实现,并拿出来与大家一同分享;
各大开放平台注册账户获取AppKey和AppSecret
Mob开放平台地址:http://www.mob.com/#/index
QQ开放平台地址:http://open.qq.com/
新浪微博开放平台地址:http://open.weibo.com/
微信开放平台地址:https://open.weixin.qq.com/
百度开放平台地址:http://apps.bdimg.com/
注:
一、关于各大开放平台的开发者用户注册和创建应用获取AppKey、AppSecret我就不多说了,对于大家来说这都so easy;
二、Eclipse在创建应用填写包名应该是manifest里面的packpage:
studio的则是build.gradle里面的applicationId:
一般情况下,studio的applicationId和manifest下的包名是一致的,但是applicationId是手动改变的,所以注意在创建应用的时候,要使用applicationId。
新浪微博第三方登录实现:
在做新浪微博第三方登录的时候,参考他们的api文档,感觉他们的api文档写的特别的乱,并且下载的Demo运行还报错,以至于浪费了我大半天的时间,去网上搜了几篇关于第三方登录的文章,写的并不全,时代也比较久远,并且基本没有相关Demo下载,有的就是拷贝的新浪微博的开放平台的原文,我也是醉了…..
由于时间比较紧迫,所以并没有再继续深究下去。大家应该都知道有许多平台把例如分享、登录等功能集成好了,实现功能十分简单,这一次我是利用的Mob移动开发平台http://www.mob.com/#/index 实现的新浪微博第三方登录,感觉还不错实现起来比较简单。
一、下载Mob平台的SDK
首先下载SDK,得到我们需要的资源
SDK地址(http://www.mob.com/#/downloadDetail/ShareSDK/android),下载下来发现无法解压(你们可以试一下),联系他们客服,他们说:上传错误,就给我发了一个。我把它和我的Dmeo放在一起,可供下载。
找到ShareSDK for Android包下的QuickIntegrater.jar文件双击:
感觉还不错,各大平台都有,只需要选择新浪微博就可以(我感觉还是没包装过的比较好,所以另外三种登录方式没采用这种方法),解压之后生成一个Sample文件:
我们需要把里面的东西全部拿到我们的studio项目中,libs、res和src里面的全部拷贝复制就可以,studio新建assets的方法和Eclipse不一样,所以给大家发个图:
点击创建就可以了,再把assets下的ShareSDK.xml拷贝就去;
其次配置ShareSDK
把ShareSDK拷贝进去之后,我们需要把ShareSDK.xml文件中关于Mob(ShareSDK)和新浪微博(SinaWeibo)的Appkey、AppSecret和RedirectUrl替换成我们申请的值,如下图:
把红箭头中的ShareSDK中的AppKey换成我们在Mob后台申请的AppKey;
红框框中的SinaWeibo中的AppKey、AppSecret和RedirectUrl换成我们在新浪微博后台申请的对应的值;
RedirectUrl这个字段的值就是我们后台项目中的应用信息——>高级信息——>OAuth2.0 授权设置中的授权回调页这个字段,需要我们填写一个有效的地址就可以,例如下图:
注:如果RedirectUrl这个字段不填或填写不对会报以下错误:
二、配置AndroidManifest.xml
添加activity信息
<strong><span style="font-size:24px;"><code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">activity </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.mob.tools.MobUIShell"</span> <span class="hljs-attribute">android:theme</span>=<span class="hljs-value">"@android:style/Theme.Translucent.NoTitleBar"</span> <span class="hljs-attribute">android:configChanges</span>=<span class="hljs-value">"keyboardHidden|orientation|screenSize"</span> <span class="hljs-attribute">android:screenOrientation</span>=<span class="hljs-value">"portrait"</span> <span class="hljs-attribute">android:windowSoftInputMode</span>=<span class="hljs-value">"stateHidden|adjustResize"</span> ></span> <span class="hljs-tag"></<span class="hljs-title">activity</span>></span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li></ul>
二、添加代码
首先是Mob的授权,在程序的入口处添加授权代码:
<strong><span style="font-size:24px;"><code class="hljs cs has-numbering"><span class="hljs-comment">//Mob平台授权</span> ShareSDK.initSDK(<span class="hljs-keyword">this</span>);</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li></ul>
其次直接调用thirdSinaLogin()方法就可以了:
<strong><span style="font-size:24px;"><code class="hljs java has-numbering"><span class="hljs-comment">//-----------------------------------------------------新浪微博授权相关--------------</span> <span class="hljs-javadoc">/** 新浪微博授权、获取用户信息页面 */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">thirdSinaLogin</span>() { <span class="hljs-comment">//初始化新浪平台</span> Platform pf = ShareSDK.getPlatform(MainActivity.<span class="hljs-keyword">this</span>, SinaWeibo.NAME); pf.SSOSetting(<span class="hljs-keyword">true</span>); <span class="hljs-comment">//设置监听</span> pf.setPlatformActionListener(MainActivity.<span class="hljs-keyword">this</span>); <span class="hljs-comment">//获取登陆用户的信息,如果没有授权,会先授权,然后获取用户信息</span> pf.authorize(); } <span class="hljs-javadoc">/** 新浪微博授权成功回调页面 */</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onComplete</span>(Platform platform, <span class="hljs-keyword">int</span> action, HashMap<String, Object> hashMap) { <span class="hljs-javadoc">/** res是返回的数据,例如showUser(null),返回用户信息,对其解析就行 * http://sharesdk.cn/androidDoc/cn/sharesdk/framework/PlatformActionListener.html * */</span> Message msg = <span class="hljs-keyword">new</span> Message(); msg.what = MSG_ACTION_CCALLBACK; msg.arg1 = <span class="hljs-number">1</span>; msg.arg2 = action; msg.obj = platform; UIHandler.sendMessage(msg, <span class="hljs-keyword">this</span>); } <span class="hljs-javadoc">/** 取消授权 */</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCancel</span>(Platform platform, <span class="hljs-keyword">int</span> action) { Message msg = <span class="hljs-keyword">new</span> Message(); msg.what = MSG_ACTION_CCALLBACK; msg.arg1 = <span class="hljs-number">3</span>; msg.arg2 = action; msg.obj = platform; UIHandler.sendMessage(msg, <span class="hljs-keyword">this</span>); } <span class="hljs-javadoc">/** 授权失败 */</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span>(Platform platform, <span class="hljs-keyword">int</span> action, Throwable t) { t.printStackTrace(); t.getMessage(); Message msg = <span class="hljs-keyword">new</span> Message(); msg.what = MSG_ACTION_CCALLBACK; msg.arg1 = <span class="hljs-number">2</span>; msg.arg2 = action; msg.obj = t; UIHandler.sendMessage(msg, <span class="hljs-keyword">this</span>); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">handleMessage</span>(Message msg) { <span class="hljs-keyword">switch</span>(msg.what) { <span class="hljs-keyword">case</span> MSG_TOAST: { String text = String.valueOf(msg.obj); Toast.makeText(MainActivity.<span class="hljs-keyword">this</span>, text, Toast.LENGTH_SHORT).show(); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> MSG_ACTION_CCALLBACK: { <span class="hljs-keyword">switch</span> (msg.arg1) { <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>: { <span class="hljs-comment">// 成功, successful notification</span> <span class="hljs-comment">//授权成功后,获取用户信息,要自己解析,看看oncomplete里面的注释</span> <span class="hljs-comment">//ShareSDK只保存以下这几个通用值</span> Platform pf = ShareSDK.getPlatform(MainActivity.<span class="hljs-keyword">this</span>, SinaWeibo.NAME); Log.e(<span class="hljs-string">"sharesdk use_id"</span>, pf.getDb().getUserId()); <span class="hljs-comment">//获取用户id</span> Log.e(<span class="hljs-string">"sharesdk use_name"</span>, pf.getDb().getUserName());<span class="hljs-comment">//获取用户名称</span> Log.e(<span class="hljs-string">"sharesdk use_icon"</span>, pf.getDb().getUserIcon());<span class="hljs-comment">//获取用户头像</span> mThirdLoginResult.setText(<span class="hljs-string">"授权成功"</span>+<span class="hljs-string">"\n"</span>+<span class="hljs-string">"用户id:"</span> + pf.getDb().getUserId() + <span class="hljs-string">"\n"</span> + <span class="hljs-string">"获取用户名称"</span> + pf.getDb().getUserName() + <span class="hljs-string">"\n"</span> + <span class="hljs-string">"获取用户头像"</span> + pf.getDb().getUserIcon()); <span class="hljs-comment">//mPf.author()这个方法每一次都会调用授权,出现授权界面</span> <span class="hljs-comment">//如果要删除授权信息,重新授权</span> <span class="hljs-comment">//mPf.getDb().removeAccount();</span> <span class="hljs-comment">//调用后,用户就得重新授权,否则下一次就不用授权</span> } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>: { mThirdLoginResult.setText(<span class="hljs-string">"登录失败"</span>); } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>: { <span class="hljs-comment">// 取消, cancel notification</span> mThirdLoginResult.setText(<span class="hljs-string">"取消授权"</span>); } <span class="hljs-keyword">break</span>; } } <span class="hljs-keyword">break</span>; <span class="hljs-keyword">case</span> MSG_CANCEL_NOTIFY: { NotificationManager nm = (NotificationManager) msg.obj; <span class="hljs-keyword">if</span> (nm != <span class="hljs-keyword">null</span>) { nm.cancel(msg.arg1); } } <span class="hljs-keyword">break</span>; } <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>; }</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li><li><strong><span style="font-size:24px;">21</span></strong></li><li><strong><span style="font-size:24px;">22</span></strong></li><li><strong><span style="font-size:24px;">23</span></strong></li><li><strong><span style="font-size:24px;">24</span></strong></li><li><strong><span style="font-size:24px;">25</span></strong></li><li><strong><span style="font-size:24px;">26</span></strong></li><li><strong><span style="font-size:24px;">27</span></strong></li><li><strong><span style="font-size:24px;">28</span></strong></li><li><strong><span style="font-size:24px;">29</span></strong></li><li><strong><span style="font-size:24px;">30</span></strong></li><li><strong><span style="font-size:24px;">31</span></strong></li><li><strong><span style="font-size:24px;">32</span></strong></li><li><strong><span style="font-size:24px;">33</span></strong></li><li><strong><span style="font-size:24px;">34</span></strong></li><li><strong><span style="font-size:24px;">35</span></strong></li><li><strong><span style="font-size:24px;">36</span></strong></li><li><strong><span style="font-size:24px;">37</span></strong></li><li><strong><span style="font-size:24px;">38</span></strong></li><li><strong><span style="font-size:24px;">39</span></strong></li><li><strong><span style="font-size:24px;">40</span></strong></li><li><strong><span style="font-size:24px;">41</span></strong></li><li><strong><span style="font-size:24px;">42</span></strong></li><li><strong><span style="font-size:24px;">43</span></strong></li><li><strong><span style="font-size:24px;">44</span></strong></li><li><strong><span style="font-size:24px;">45</span></strong></li><li><strong><span style="font-size:24px;">46</span></strong></li><li><strong><span style="font-size:24px;">47</span></strong></li><li><strong><span style="font-size:24px;">48</span></strong></li><li><strong><span style="font-size:24px;">49</span></strong></li><li><strong><span style="font-size:24px;">50</span></strong></li><li><strong><span style="font-size:24px;">51</span></strong></li><li><strong><span style="font-size:24px;">52</span></strong></li><li><strong><span style="font-size:24px;">53</span></strong></li><li><strong><span style="font-size:24px;">54</span></strong></li><li><strong><span style="font-size:24px;">55</span></strong></li><li><strong><span style="font-size:24px;">56</span></strong></li><li><strong><span style="font-size:24px;">57</span></strong></li><li><strong><span style="font-size:24px;">58</span></strong></li><li><strong><span style="font-size:24px;">59</span></strong></li><li><strong><span style="font-size:24px;">60</span></strong></li><li><strong><span style="font-size:24px;">61</span></strong></li><li><strong><span style="font-size:24px;">62</span></strong></li><li><strong><span style="font-size:24px;">63</span></strong></li><li><strong><span style="font-size:24px;">64</span></strong></li><li><strong><span style="font-size:24px;">65</span></strong></li><li><strong><span style="font-size:24px;">66</span></strong></li><li><strong><span style="font-size:24px;">67</span></strong></li><li><strong><span style="font-size:24px;">68</span></strong></li><li><strong><span style="font-size:24px;">69</span></strong></li><li><strong><span style="font-size:24px;">70</span></strong></li><li><strong><span style="font-size:24px;">71</span></strong></li><li><strong><span style="font-size:24px;">72</span></strong></li><li><strong><span style="font-size:24px;">73</span></strong></li><li><strong><span style="font-size:24px;">74</span></strong></li><li><strong><span style="font-size:24px;">75</span></strong></li><li><strong><span style="font-size:24px;">76</span></strong></li><li><strong><span style="font-size:24px;">77</span></strong></li><li><strong><span style="font-size:24px;">78</span></strong></li><li><strong><span style="font-size:24px;">79</span></strong></li><li><strong><span style="font-size:24px;">80</span></strong></li><li><strong><span style="font-size:24px;">81</span></strong></li><li><strong><span style="font-size:24px;">82</span></strong></li><li><strong><span style="font-size:24px;">83</span></strong></li><li><strong><span style="font-size:24px;">84</span></strong></li><li><strong><span style="font-size:24px;">85</span></strong></li><li><strong><span style="font-size:24px;">86</span></strong></li><li><strong><span style="font-size:24px;">87</span></strong></li><li><strong><span style="font-size:24px;">88</span></strong></li><li><strong><span style="font-size:24px;">89</span></strong></li><li><strong><span style="font-size:24px;">90</span></strong></li><li><strong><span style="font-size:24px;">91</span></strong></li><li><strong><span style="font-size:24px;">92</span></strong></li><li><strong><span style="font-size:24px;">93</span></strong></li><li><strong><span style="font-size:24px;">94</span></strong></li><li><strong><span style="font-size:24px;">95</span></strong></li></ul>
最后退出登录
<strong><span style="font-size:24px;"><code class="hljs avrasm has-numbering"> Platform mPf = ShareSDK<span class="hljs-preprocessor">.getPlatform</span>(MainActivity<span class="hljs-preprocessor">.this</span>, SinaWeibo<span class="hljs-preprocessor">.NAME</span>)<span class="hljs-comment">;</span> //如果要删除授权信息,重新授权 ShareSDK<span class="hljs-preprocessor">.removeCookieOnAuthorize</span>(true)<span class="hljs-comment">;</span> mPf<span class="hljs-preprocessor">.removeAccount</span>()<span class="hljs-comment">;</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li></ul>
到这里关于新浪微博第三方登录的就基本结束了,我们获取到了用户的id、昵称、头像地址等信息,使用唯一值id再结合自己服务器的接口,便可实现第三方登录了.效果图如下:
注意:如果你想修改授权的登录页面,可以参考
这个链接里面的第11条
QQ第三方登录实现:
注意
一、我不建议大家去看QQ开放平台关于获取个人信息那部分的文档,因为他们的文档已经过时了,我当时按照文档做过,但是一直报错,后来问客服,他告诉我他们的文档它久远了,已经不能用了<当时我就醉了、、>,而是给了我一份Demo,我会把Demo放在里面供大家下载。
二、大家使用的测试机一定要是自动获取的网络时间,否则得不到信息,并且也没有错误信息,我研究了半天的时间,也没找到到底为什么,一问客服才知道,服务器时间和请求时间戳要相同才可以<我又是醉了、、、>。
三、大家要打包APP再进行测试。
QQ第三方登录的步骤分为:
1.先登录成功获取token和openid
2.再通过token和openid获取用户的信息。
一、导入SDK的jar文件
把这两个jar文件导入项目中
二、配置AndroidManifest
<strong><span style="font-size:24px;"><code class="hljs xml has-numbering"><span class="hljs-comment"><!-- QQ第三方登录相关 开始 --></span> <span class="hljs-tag"><<span class="hljs-title">activity </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.tencent.tauth.AuthActivity"</span> <span class="hljs-attribute">android:launchMode</span>=<span class="hljs-value">"singleTask"</span> <span class="hljs-attribute">android:noHistory</span>=<span class="hljs-value">"true"</span> ></span> <span class="hljs-tag"><<span class="hljs-title">intent-filter</span>></span> <span class="hljs-tag"><<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.action.VIEW"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">category</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.category.DEFAULT"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">category</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"android.intent.category.BROWSABLE"</span> /></span> <span class="hljs-tag"><<span class="hljs-title">data</span> <span class="hljs-attribute">android:scheme</span>=<span class="hljs-value">"tencent你的APPId"</span> /></span> <span class="hljs-tag"></<span class="hljs-title">intent-filter</span>></span> <span class="hljs-tag"></<span class="hljs-title">activity</span>></span> <span class="hljs-tag"><<span class="hljs-title">activity</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.tencent.connect.common.AssistActivity"</span> <span class="hljs-attribute">android:theme</span>=<span class="hljs-value">"@android:style/Theme.Translucent.NoTitleBar"</span> <span class="hljs-attribute">android:configChanges</span>=<span class="hljs-value">"orientation|keyboardHidden|screenSize"</span> /></span> <span class="hljs-comment"><!-- QQ第三方登录相关 结束 --></span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li></ul>
在scheme这个字段输入你申请的appid,tencent不要删掉。
三、添加代码
首先在程序入口添加:
<strong><span style="font-size:24px;"><code class="hljs avrasm has-numbering">Tencent mTencent = Tencent<span class="hljs-preprocessor">.createInstance</span>(你的AppId, this<span class="hljs-preprocessor">.getApplicationContext</span>())<span class="hljs-comment">;</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li></ul>
剩下的调用loginQQ方法便可以:
<strong><span style="font-size:24px;"><code class="hljs java has-numbering"><span class="hljs-javadoc">/** ------------------------QQ第三方登录-------------------- */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">loginQQ</span>(){ <span class="hljs-javadoc">/** 判断是否登陆过 */</span> <span class="hljs-keyword">if</span> (!mTencent.isSessionValid()){ mTencent.login(<span class="hljs-keyword">this</span>, <span class="hljs-string">"all"</span>,loginListener); }<span class="hljs-javadoc">/** 登陆过注销之后在登录 */</span> <span class="hljs-keyword">else</span> { mTencent.logout(<span class="hljs-keyword">this</span>); mTencent.login(<span class="hljs-keyword">this</span>, <span class="hljs-string">"all"</span>,loginListener); } } IUiListener loginListener = <span class="hljs-keyword">new</span> BaseUiListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doComplete</span>(JSONObject values) { initOpenidAndToken(values); updateUserInfo(); } }; <span class="hljs-javadoc">/** QQ登录第二步:存储token和openid */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">initOpenidAndToken</span>(JSONObject jsonObject) { <span class="hljs-keyword">try</span> { String token = jsonObject.getString(Constants.PARAM_ACCESS_TOKEN); String expires = jsonObject.getString(Constants.PARAM_EXPIRES_IN); String openId = jsonObject.getString(Constants.PARAM_OPEN_ID); <span class="hljs-keyword">if</span> (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(openId)) { mTencent.setAccessToken(token, expires); mTencent.setOpenId(openId); } } <span class="hljs-keyword">catch</span>(Exception e) { } } <span class="hljs-javadoc">/** QQ登录第三步:获取用户信息 */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">updateUserInfo</span>() { <span class="hljs-keyword">if</span> (mTencent != <span class="hljs-keyword">null</span> && mTencent.isSessionValid()) { IUiListener listener = <span class="hljs-keyword">new</span> IUiListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span>(UiError e) { Message msg = <span class="hljs-keyword">new</span> Message(); msg.obj = <span class="hljs-string">"把手机时间改成获取网络时间"</span>; msg.what = <span class="hljs-number">1</span>; mHandler.sendMessage(msg); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onComplete</span>(<span class="hljs-keyword">final</span> Object response) { Message msg = <span class="hljs-keyword">new</span> Message(); msg.obj = response; msg.what = <span class="hljs-number">0</span>; mHandler.sendMessage(msg); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCancel</span>() { Message msg = <span class="hljs-keyword">new</span> Message(); msg.obj = <span class="hljs-string">"获取用户信息失败"</span>; msg.what = <span class="hljs-number">2</span>; mHandler.sendMessage(msg); } }; mInfo = <span class="hljs-keyword">new</span> UserInfo(<span class="hljs-keyword">this</span>, mTencent.getQQToken()); mInfo.getUserInfo(listener); } <span class="hljs-keyword">else</span> { } } Handler mHandler = <span class="hljs-keyword">new</span> Handler() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">handleMessage</span>(Message msg) { <span class="hljs-javadoc">/** 获取用户信息成功 */</span> <span class="hljs-keyword">if</span> (msg.what == <span class="hljs-number">0</span>) { JSONObject response = (JSONObject) msg.obj; <span class="hljs-keyword">if</span> (response.has(<span class="hljs-string">"nickname"</span>)) { <span class="hljs-keyword">try</span> { log(<span class="hljs-string">"获取用户信息成功,返回结果:"</span>+response.toString()); mThirdLoginResult.setText(<span class="hljs-string">"登录成功\n"</span>+<span class="hljs-string">"用户id:"</span>+openid+<span class="hljs-string">"\n昵称:"</span>+response.getString(<span class="hljs-string">"nickname"</span>)+<span class="hljs-string">"\n头像地址:"</span>+response.get(<span class="hljs-string">"figureurl_qq_1"</span>)); } <span class="hljs-keyword">catch</span> (JSONException e) { e.printStackTrace(); } } }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(msg.what == <span class="hljs-number">1</span>){ mThirdLoginResult.setText(msg+<span class="hljs-string">""</span>); }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(msg.what == <span class="hljs-number">2</span>){ mThirdLoginResult.setText(msg+<span class="hljs-string">""</span>); } } }; <span class="hljs-javadoc">/** QQ登录第一步:获取token和openid */</span> <span class="hljs-keyword">private</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BaseUiListener</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">IUiListener</span> {</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onComplete</span>(Object response) { <span class="hljs-keyword">if</span> (<span class="hljs-keyword">null</span> == response) { mToast(<span class="hljs-string">"登录失败"</span>); <span class="hljs-keyword">return</span>; } JSONObject jsonResponse = (JSONObject) response; <span class="hljs-keyword">if</span> (<span class="hljs-keyword">null</span> != jsonResponse && jsonResponse.length() == <span class="hljs-number">0</span>) { mToast(<span class="hljs-string">"登录失败"</span>); <span class="hljs-keyword">return</span>; } log(<span class="hljs-string">"QQ登录成功返回结果-"</span> + response.toString()); doComplete((JSONObject)response); } <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doComplete</span>(JSONObject response) {} <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span>(UiError e) { Util.toastMessage(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"onError: "</span> + e.errorDetail); Util.dismissDialog(); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCancel</span>() { Util.toastMessage(MainActivity.<span class="hljs-keyword">this</span>, <span class="hljs-string">"onCancel: "</span>); Util.dismissDialog(); <span class="hljs-keyword">if</span> (isServerSideLogin) { isServerSideLogin = <span class="hljs-keyword">false</span>; } } } <span class="hljs-javadoc">/** -------------------------QQ第三方登录结束-------------------- */</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li><li><strong><span style="font-size:24px;">21</span></strong></li><li><strong><span style="font-size:24px;">22</span></strong></li><li><strong><span style="font-size:24px;">23</span></strong></li><li><strong><span style="font-size:24px;">24</span></strong></li><li><strong><span style="font-size:24px;">25</span></strong></li><li><strong><span style="font-size:24px;">26</span></strong></li><li><strong><span style="font-size:24px;">27</span></strong></li><li><strong><span style="font-size:24px;">28</span></strong></li><li><strong><span style="font-size:24px;">29</span></strong></li><li><strong><span style="font-size:24px;">30</span></strong></li><li><strong><span style="font-size:24px;">31</span></strong></li><li><strong><span style="font-size:24px;">32</span></strong></li><li><strong><span style="font-size:24px;">33</span></strong></li><li><strong><span style="font-size:24px;">34</span></strong></li><li><strong><span style="font-size:24px;">35</span></strong></li><li><strong><span style="font-size:24px;">36</span></strong></li><li><strong><span style="font-size:24px;">37</span></strong></li><li><strong><span style="font-size:24px;">38</span></strong></li><li><strong><span style="font-size:24px;">39</span></strong></li><li><strong><span style="font-size:24px;">40</span></strong></li><li><strong><span style="font-size:24px;">41</span></strong></li><li><strong><span style="font-size:24px;">42</span></strong></li><li><strong><span style="font-size:24px;">43</span></strong></li><li><strong><span style="font-size:24px;">44</span></strong></li><li><strong><span style="font-size:24px;">45</span></strong></li><li><strong><span style="font-size:24px;">46</span></strong></li><li><strong><span style="font-size:24px;">47</span></strong></li><li><strong><span style="font-size:24px;">48</span></strong></li><li><strong><span style="font-size:24px;">49</span></strong></li><li><strong><span style="font-size:24px;">50</span></strong></li><li><strong><span style="font-size:24px;">51</span></strong></li><li><strong><span style="font-size:24px;">52</span></strong></li><li><strong><span style="font-size:24px;">53</span></strong></li><li><strong><span style="font-size:24px;">54</span></strong></li><li><strong><span style="font-size:24px;">55</span></strong></li><li><strong><span style="font-size:24px;">56</span></strong></li><li><strong><span style="font-size:24px;">57</span></strong></li><li><strong><span style="font-size:24px;">58</span></strong></li><li><strong><span style="font-size:24px;">59</span></strong></li><li><strong><span style="font-size:24px;">60</span></strong></li><li><strong><span style="font-size:24px;">61</span></strong></li><li><strong><span style="font-size:24px;">62</span></strong></li><li><strong><span style="font-size:24px;">63</span></strong></li><li><strong><span style="font-size:24px;">64</span></strong></li><li><strong><span style="font-size:24px;">65</span></strong></li><li><strong><span style="font-size:24px;">66</span></strong></li><li><strong><span style="font-size:24px;">67</span></strong></li><li><strong><span style="font-size:24px;">68</span></strong></li><li><strong><span style="font-size:24px;">69</span></strong></li><li><strong><span style="font-size:24px;">70</span></strong></li><li><strong><span style="font-size:24px;">71</span></strong></li><li><strong><span style="font-size:24px;">72</span></strong></li><li><strong><span style="font-size:24px;">73</span></strong></li><li><strong><span style="font-size:24px;">74</span></strong></li><li><strong><span style="font-size:24px;">75</span></strong></li><li><strong><span style="font-size:24px;">76</span></strong></li><li><strong><span style="font-size:24px;">77</span></strong></li><li><strong><span style="font-size:24px;">78</span></strong></li><li><strong><span style="font-size:24px;">79</span></strong></li><li><strong><span style="font-size:24px;">80</span></strong></li><li><strong><span style="font-size:24px;">81</span></strong></li><li><strong><span style="font-size:24px;">82</span></strong></li><li><strong><span style="font-size:24px;">83</span></strong></li><li><strong><span style="font-size:24px;">84</span></strong></li><li><strong><span style="font-size:24px;">85</span></strong></li><li><strong><span style="font-size:24px;">86</span></strong></li><li><strong><span style="font-size:24px;">87</span></strong></li><li><strong><span style="font-size:24px;">88</span></strong></li><li><strong><span style="font-size:24px;">89</span></strong></li><li><strong><span style="font-size:24px;">90</span></strong></li><li><strong><span style="font-size:24px;">91</span></strong></li><li><strong><span style="font-size:24px;">92</span></strong></li><li><strong><span style="font-size:24px;">93</span></strong></li><li><strong><span style="font-size:24px;">94</span></strong></li><li><strong><span style="font-size:24px;">95</span></strong></li><li><strong><span style="font-size:24px;">96</span></strong></li><li><strong><span style="font-size:24px;">97</span></strong></li><li><strong><span style="font-size:24px;">98</span></strong></li><li><strong><span style="font-size:24px;">99</span></strong></li><li><strong><span style="font-size:24px;">100</span></strong></li><li><strong><span style="font-size:24px;">101</span></strong></li><li><strong><span style="font-size:24px;">102</span></strong></li><li><strong><span style="font-size:24px;">103</span></strong></li><li><strong><span style="font-size:24px;">104</span></strong></li><li><strong><span style="font-size:24px;">105</span></strong></li><li><strong><span style="font-size:24px;">106</span></strong></li><li><strong><span style="font-size:24px;">107</span></strong></li><li><strong><span style="font-size:24px;">108</span></strong></li><li><strong><span style="font-size:24px;">109</span></strong></li><li><strong><span style="font-size:24px;">110</span></strong></li><li><strong><span style="font-size:24px;">111</span></strong></li><li><strong><span style="font-size:24px;">112</span></strong></li><li><strong><span style="font-size:24px;">113</span></strong></li><li><strong><span style="font-size:24px;">114</span></strong></li><li><strong><span style="font-size:24px;">115</span></strong></li><li><strong><span style="font-size:24px;">116</span></strong></li><li><strong><span style="font-size:24px;">117</span></strong></li><li><strong><span style="font-size:24px;">118</span></strong></li></ul>
代码有注释,大家看一下应该很容易明白。
QQ可以获得以下相关信息:
大家只要response.getString(“字段名”)就可以得到数据了:
<strong><span style="font-size:24px;"><code class="hljs json has-numbering">{ "<span class="hljs-attribute">is_yellow_year_vip</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>, "<span class="hljs-attribute">ret</span>": <span class="hljs-value"><span class="hljs-number">0</span></span>, "<span class="hljs-attribute">figureurl_qq_1</span>": <span class="hljs-value"><span class="hljs-string">"http://q.qlogo.cn/qqapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/40"</span></span>, "<span class="hljs-attribute">figureurl_qq_2</span>": <span class="hljs-value"><span class="hljs-string">"http://q.qlogo.cn/qqapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/100"</span></span>, "<span class="hljs-attribute">nickname</span>": <span class="hljs-value"><span class="hljs-string">"小罗"</span></span>, "<span class="hljs-attribute">yellow_vip_level</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>, "<span class="hljs-attribute">msg</span>": <span class="hljs-value"><span class="hljs-string">""</span></span>, "<span class="hljs-attribute">figureurl_1</span>": <span class="hljs-value"><span class="hljs-string">"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/50"</span></span>, "<span class="hljs-attribute">vip</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>, "<span class="hljs-attribute">level</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>, "<span class="hljs-attribute">figureurl_2</span>": <span class="hljs-value"><span class="hljs-string">"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/100"</span></span>, "<span class="hljs-attribute">is_yellow_vip</span>": <span class="hljs-value"><span class="hljs-string">"0"</span></span>, "<span class="hljs-attribute">gender</span>": <span class="hljs-value"><span class="hljs-string">"男"</span></span>, "<span class="hljs-attribute">figureurl</span>": <span class="hljs-value"><span class="hljs-string">"http://qzapp.qlogo.cn/qzapp/222222/8C75BBE3DC6B0E9A64BD31449A3C8CB0/30"</span> </span>}</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li></ul>
最后退出登录:
<strong><span style="font-size:24px;"><code class="hljs cs has-numbering">mTencent.logout(<span class="hljs-keyword">this</span>);</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li></ul>
给大家看一下截图:
微信第三方登录
微信平台还是做得挺不错的,大家可以看一下他们的文档,只不过我们需要自己写网络请求(衰衰衰衰衰)
微信第三方登录分为:
1.获取code
2.根据code获取token
3.根据token获取用户信息
在第二、三步需要我们自己去写网络请求,我也是醉了。。。。
一、导入SDK的libammsdk.jar文件,我就不多说了
二、配置AndroidManifest
在你的build.gradle的applicationId相应目录下新建一个wxapi目录,在该wxapi目录下新增一个WXEntryActivity类,该类继承自Activity(例如应用程序build.gradle的applicationId为com.aohuan.jinghai_lifee,则新添加的类如下图所示)
并在manifest文件里面加上exported属性,设置为true,例如:
<strong><span style="font-size:24px;"><code class="hljs xml has-numbering"><span class="hljs-tag"><<span class="hljs-title">activity </span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.aohuan.jinghai_lifee.wxapi.WXEntryActivity"</span> <span class="hljs-attribute">android:exported</span>=<span class="hljs-value">"true"</span> <span class="hljs-attribute">android:label</span>=<span class="hljs-value">"@string/app_name"</span> ></span> <span class="hljs-tag"></<span class="hljs-title">activity</span>></span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li></ul>
三、添加代码
<strong><span style="font-size:24px;"><code class="hljs java has-numbering"><span class="hljs-javadoc">/** -------------------------微信第三方登录---------------------- */</span> <span class="hljs-javadoc">/** * * 微信平台应用授权登录接入代码示例 * * */</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">regToWx</span>(){ <span class="hljs-comment">// 通过WXAPIFactory工厂,获得IWXAPI的实例</span> api = WXAPIFactory.createWXAPI(MainActivity.<span class="hljs-keyword">this</span>, AllApk.WEIXIN_APP_ID, <span class="hljs-keyword">true</span>); <span class="hljs-comment">// 将应用的appid注册到微信</span> api.registerApp(AllApk.WEIXIN_APP_ID); } <span class="hljs-comment">//获取微信访问getCode</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getCode</span>(){ <span class="hljs-keyword">final</span> SendAuth.Req req = <span class="hljs-keyword">new</span> SendAuth.Req(); req.scope = <span class="hljs-string">"snsapi_userinfo"</span>; req.state = <span class="hljs-string">"carjob_wx_login"</span>; api.sendReq(req); } <span class="hljs-javadoc">/** -------------------------微信第三方登录结束-------------------- */</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li></ul>
首先在onCreate()方法里面调用regToWx()方法实现微信平台的注册;
其次第一步获取code
调用getCode()方法发送获取code的请求,接受code则是在WXEntryActivity这个类里面的onResp():
第二步使用AsyncTask获取token:
<strong><span style="font-size:24px;"><code class="hljs scala has-numbering"><span class="hljs-javadoc">/** 微信登录第二步:获取token */</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsynctaskToken</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AsyncTask</span><<span class="hljs-title">Object</span> , <span class="hljs-title">Object</span> , <span class="hljs-title">Object</span>> {</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> Object doInBackground(Object... params) { HttpGet httpRequest = <span class="hljs-keyword">new</span> HttpGet(params[<span class="hljs-number">0</span>].toString()); <span class="hljs-keyword">try</span>{ HttpClient httpClient = <span class="hljs-keyword">new</span> DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest); <span class="hljs-keyword">if</span>(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ BaseActivity.log(<span class="hljs-string">"请求个人信息成功"</span>); String strResult = EntityUtils.toString(httpResponse.getEntity()); <span class="hljs-keyword">return</span> strResult; } <span class="hljs-keyword">else</span>{ BaseActivity.log(<span class="hljs-string">"请求个人信息失败"</span>); <span class="hljs-keyword">return</span> <span class="hljs-string">"请求出错"</span>; } } <span class="hljs-keyword">catch</span>(ClientProtocolException e){ } <span class="hljs-keyword">catch</span> (IOException e) { e.printStackTrace(); } <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>; } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onCancelled() { <span class="hljs-keyword">super</span>.onCancelled(); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onPostExecute(Object o) { <span class="hljs-keyword">super</span>.onPostExecute(o); Object obj = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">try</span> { obj = JsonUtil.toObjectByJson(o.toString(), WeiXinLoginGetTokenBean.<span class="hljs-keyword">class</span>); } <span class="hljs-keyword">catch</span> (IOException e) { e.printStackTrace(); } WeiXinLoginGetTokenBean bean = (WeiXinLoginGetTokenBean)obj; BaseActivity.log(<span class="hljs-string">"获取token成功:\n"</span> + <span class="hljs-string">"token:"</span>+bean.getAccess_token()+<span class="hljs-string">"\nopenid"</span>+bean.getOpenid()); String url = <span class="hljs-string">"https://api.weixin.qq.com/sns/userinfo?"</span>+<span class="hljs-string">"access_token="</span>+bean.getAccess_token()+<span class="hljs-string">"&openid="</span>+bean.getOpenid(); <span class="hljs-keyword">new</span> AsynctaskInfo().execute(url); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onProgressUpdate(Object... values) { <span class="hljs-keyword">super</span>.onProgressUpdate(values); } }</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li><li><strong><span style="font-size:24px;">21</span></strong></li><li><strong><span style="font-size:24px;">22</span></strong></li><li><strong><span style="font-size:24px;">23</span></strong></li><li><strong><span style="font-size:24px;">24</span></strong></li><li><strong><span style="font-size:24px;">25</span></strong></li><li><strong><span style="font-size:24px;">26</span></strong></li><li><strong><span style="font-size:24px;">27</span></strong></li><li><strong><span style="font-size:24px;">28</span></strong></li><li><strong><span style="font-size:24px;">29</span></strong></li><li><strong><span style="font-size:24px;">30</span></strong></li><li><strong><span style="font-size:24px;">31</span></strong></li><li><strong><span style="font-size:24px;">32</span></strong></li><li><strong><span style="font-size:24px;">33</span></strong></li><li><strong><span style="font-size:24px;">34</span></strong></li><li><strong><span style="font-size:24px;">35</span></strong></li><li><strong><span style="font-size:24px;">36</span></strong></li><li><strong><span style="font-size:24px;">37</span></strong></li><li><strong><span style="font-size:24px;">38</span></strong></li><li><strong><span style="font-size:24px;">39</span></strong></li><li><strong><span style="font-size:24px;">40</span></strong></li><li><strong><span style="font-size:24px;">41</span></strong></li><li><strong><span style="font-size:24px;">42</span></strong></li><li><strong><span style="font-size:24px;">43</span></strong></li><li><strong><span style="font-size:24px;">44</span></strong></li><li><strong><span style="font-size:24px;">45</span></strong></li><li><strong><span style="font-size:24px;">46</span></strong></li><li><strong><span style="font-size:24px;">47</span></strong></li><li><strong><span style="font-size:24px;">48</span></strong></li><li><strong><span style="font-size:24px;">49</span></strong></li></ul>
第三步获取用户信息:
<strong><span style="font-size:24px;"><code class="hljs scala has-numbering"><span class="hljs-javadoc">/** 微信登录第三步:获取用户信息 */</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsynctaskInfo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">AsyncTask</span><<span class="hljs-title">Object</span> , <span class="hljs-title">Object</span> , <span class="hljs-title">Object</span>> {</span> <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> Object doInBackground(Object... params) { HttpGet httpRequest = <span class="hljs-keyword">new</span> HttpGet(params[<span class="hljs-number">0</span>].toString()); <span class="hljs-keyword">try</span>{ HttpClient httpClient = <span class="hljs-keyword">new</span> DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest); <span class="hljs-keyword">if</span>(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ BaseActivity.log(<span class="hljs-string">"请求个人信息成功"</span>); String strResult = EntityUtils.toString(httpResponse.getEntity()); <span class="hljs-keyword">return</span> strResult; } <span class="hljs-keyword">else</span>{ BaseActivity.log(<span class="hljs-string">"请求个人信息失败"</span>); <span class="hljs-keyword">return</span> <span class="hljs-string">"请求出错"</span>; } } <span class="hljs-keyword">catch</span>(ClientProtocolException e){ } <span class="hljs-keyword">catch</span> (IOException e) { e.printStackTrace(); } <span class="hljs-keyword">return</span> <span class="hljs-keyword">null</span>; } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onCancelled() { <span class="hljs-keyword">super</span>.onCancelled(); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onPostExecute(Object o) { <span class="hljs-keyword">super</span>.onPostExecute(o); Object obj = <span class="hljs-keyword">null</span>; <span class="hljs-keyword">try</span> { obj = JsonUtil.toObjectByJson(o.toString(), WeiXinLoginGetUserinfoBean.<span class="hljs-keyword">class</span>); } <span class="hljs-keyword">catch</span> (IOException e) { e.printStackTrace(); } WeiXinLoginGetUserinfoBean bean = (WeiXinLoginGetUserinfoBean)obj; BaseActivity.log(<span class="hljs-string">"获取用户信息成功:\n"</span> + <span class="hljs-string">"昵称:"</span>+bean.getNickname()+<span class="hljs-string">"\n头像路径"</span>+bean.getHeadimgurl()); Toast.makeText(mContext,<span class="hljs-string">"获取用户信息成功:\n"</span>+<span class="hljs-string">"昵称:"</span>+bean.getNickname() + <span class="hljs-string">"\n头像路径:"</span>+bean.getHeadimgurl(),Toast.LENGTH_LONG).show(); finish(); } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> void onProgressUpdate(Object... values) { <span class="hljs-keyword">super</span>.onProgressUpdate(values); } }</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li><li><strong><span style="font-size:24px;">21</span></strong></li><li><strong><span style="font-size:24px;">22</span></strong></li><li><strong><span style="font-size:24px;">23</span></strong></li><li><strong><span style="font-size:24px;">24</span></strong></li><li><strong><span style="font-size:24px;">25</span></strong></li><li><strong><span style="font-size:24px;">26</span></strong></li><li><strong><span style="font-size:24px;">27</span></strong></li><li><strong><span style="font-size:24px;">28</span></strong></li><li><strong><span style="font-size:24px;">29</span></strong></li><li><strong><span style="font-size:24px;">30</span></strong></li><li><strong><span style="font-size:24px;">31</span></strong></li><li><strong><span style="font-size:24px;">32</span></strong></li><li><strong><span style="font-size:24px;">33</span></strong></li><li><strong><span style="font-size:24px;">34</span></strong></li><li><strong><span style="font-size:24px;">35</span></strong></li><li><strong><span style="font-size:24px;">36</span></strong></li><li><strong><span style="font-size:24px;">37</span></strong></li><li><strong><span style="font-size:24px;">38</span></strong></li><li><strong><span style="font-size:24px;">39</span></strong></li><li><strong><span style="font-size:24px;">40</span></strong></li><li><strong><span style="font-size:24px;">41</span></strong></li><li><strong><span style="font-size:24px;">42</span></strong></li><li><strong><span style="font-size:24px;">43</span></strong></li><li><strong><span style="font-size:24px;">44</span></strong></li><li><strong><span style="font-size:24px;">45</span></strong></li><li><strong><span style="font-size:24px;">46</span></strong></li><li><strong><span style="font-size:24px;">47</span></strong></li><li><strong><span style="font-size:24px;">48</span></strong></li><li><strong><span style="font-size:24px;">49</span></strong></li></ul>
微信第三方登录就结束了,相对来说不算太复杂:
注:测试微信登录的时候,也需要打包测试,否则得不到数据;
百度第三方登录
百度第三方登录,相对来说就比较简单了
第一步:导入Baidu-Frontia-Full-Debug-2.0.6.jar包
第二步:配置AndroidManifest.xml
添加android:name=”com.baidu.frontia.ForntiaApplication”
第三步:添加代码
在程序入口初始化Fronta”
<strong><span style="font-size:24px;"><code class="hljs avrasm has-numbering">Frontia<span class="hljs-preprocessor">.init</span>(this<span class="hljs-preprocessor">.getApplicationContext</span>(),你的AppKey)<span class="hljs-comment">;</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li></ul>
注册百度:
<strong><span style="font-size:24px;"><code class="hljs fix has-numbering"><span class="hljs-attribute">//注册百度 FrontiaAuthorization mAuthorization </span>=<span class="hljs-string"> Frontia.getAuthorization();</span></code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li></ul>
主要代码直接调用baiduLogin()方法就可以:
<strong><span style="font-size:24px;"><code class="hljs java has-numbering"><span class="hljs-javadoc">/** * 百度第三方登录相关 */</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">baiduLogin</span>() { ArrayList<String> scope = <span class="hljs-keyword">new</span> ArrayList<String>(); scope.add(Scope_Basic); scope.add(Scope_Netdisk); mAuthorization.authorize(<span class="hljs-keyword">this</span>, FrontiaAuthorization.MediaType.BAIDU.toString(), scope, <span class="hljs-keyword">new</span> FrontiaAuthorizationListener.AuthorizationListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onSuccess</span>(FrontiaUser result) { <span class="hljs-keyword">if</span> (<span class="hljs-keyword">null</span> != mThirdLoginResult) { mThirdLoginResult.setText(<span class="hljs-string">"social id: "</span> + result.getId() + <span class="hljs-string">"\n"</span> + <span class="hljs-string">"token: "</span> + result.getAccessToken() + <span class="hljs-string">"\n"</span> + <span class="hljs-string">"expired: "</span> + result.getExpiresIn()); } } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onFailure</span>(<span class="hljs-keyword">int</span> errCode, String errMsg) { <span class="hljs-keyword">if</span> (<span class="hljs-keyword">null</span> != mThirdLoginResult) { mThirdLoginResult.setText(<span class="hljs-string">"errCode:"</span> + errCode + <span class="hljs-string">", errMsg:"</span> + errMsg); } } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCancel</span>() { <span class="hljs-keyword">if</span> (<span class="hljs-keyword">null</span> != mThirdLoginResult) { mThirdLoginResult.setText(<span class="hljs-string">"cancel"</span>); } } }); }</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li><li><strong><span style="font-size:24px;">12</span></strong></li><li><strong><span style="font-size:24px;">13</span></strong></li><li><strong><span style="font-size:24px;">14</span></strong></li><li><strong><span style="font-size:24px;">15</span></strong></li><li><strong><span style="font-size:24px;">16</span></strong></li><li><strong><span style="font-size:24px;">17</span></strong></li><li><strong><span style="font-size:24px;">18</span></strong></li><li><strong><span style="font-size:24px;">19</span></strong></li><li><strong><span style="font-size:24px;">20</span></strong></li><li><strong><span style="font-size:24px;">21</span></strong></li><li><strong><span style="font-size:24px;">22</span></strong></li><li><strong><span style="font-size:24px;">23</span></strong></li><li><strong><span style="font-size:24px;">24</span></strong></li><li><strong><span style="font-size:24px;">25</span></strong></li><li><strong><span style="font-size:24px;">26</span></strong></li><li><strong><span style="font-size:24px;">27</span></strong></li><li><strong><span style="font-size:24px;">28</span></strong></li></ul>
退出登录调用baiduExit()方法就可以:
<strong><span style="font-size:24px;"><code class="hljs java has-numbering"><span class="hljs-javadoc">/** * 百度第三方退出相关 */</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">baiduExit</span>() { <span class="hljs-keyword">boolean</span> result = mAuthorization.clearAuthorizationInfo(FrontiaAuthorization.MediaType.BAIDU.toString()); <span class="hljs-keyword">if</span> (result) { mThirdLoginResult.setText(<span class="hljs-string">"百度退出成功"</span>); } <span class="hljs-keyword">else</span> { mThirdLoginResult.setText(<span class="hljs-string">"百度退出失败"</span>); } }</code></span></strong><ul style="" class="pre-numbering"><li><strong><span style="font-size:24px;">1</span></strong></li><li><strong><span style="font-size:24px;">2</span></strong></li><li><strong><span style="font-size:24px;">3</span></strong></li><li><strong><span style="font-size:24px;">4</span></strong></li><li><strong><span style="font-size:24px;">5</span></strong></li><li><strong><span style="font-size:24px;">6</span></strong></li><li><strong><span style="font-size:24px;">7</span></strong></li><li><strong><span style="font-size:24px;">8</span></strong></li><li><strong><span style="font-size:24px;">9</span></strong></li><li><strong><span style="font-size:24px;">10</span></strong></li><li><strong><span style="font-size:24px;">11</span></strong></li></ul>
百度登录,挺简单的吧
1.四种登录方式已经整理好了,如果有什么地方写错了或者漏掉的,请查看我的Demo或者提出来。
2.由于这是第一次写博客,所以肯定有写的不好的地方,大家有什么意见可以提出来,相互交流沟通,共同进步。
3.由于我的源代码里面涉及到客户的信息,所以AppKey、AppSecret等信息,被我删掉了,如果想看我的Demo效果,需要修改小部分我的代码:
第一:把我com.aohuan.jinghai_lifee.wxapi换成你的applicationId(对应的包名).wxapi
第二:修改ShareSDK.xml里面对应的信息:
第三:修改manifest.xml里面对应的信息:
第四:修改AllApk里面对应的信息:
这样就可以了!!!
源码下载地址
GitHub下载地址:
https://github.com/fengmaolian/LoginAndShare