实现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信息
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>
二、添加代码
首先是Mob的授权,在程序的入口处添加授权代码:
<code class="hljs cs has-numbering"><span class="hljs-comment">//Mob平台授权</span> ShareSDK.initSDK(<span class="hljs-keyword">this</span>);</code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
其次直接调用thirdSinaLogin()方法就可以了:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li><li>82</li><li>83</li><li>84</li><li>85</li><li>86</li><li>87</li><li>88</li><li>89</li><li>90</li><li>91</li><li>92</li><li>93</li><li>94</li><li>95</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li><li>82</li><li>83</li><li>84</li><li>85</li><li>86</li><li>87</li><li>88</li><li>89</li><li>90</li><li>91</li><li>92</li><li>93</li><li>94</li><li>95</li></ul>
最后退出登录
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li></ul>
到这里关于新浪微博第三方登录的就基本结束了,我们获取到了用户的id、昵称、头像地址等信息,使用唯一值id再结合自己服务器的接口,便可实现第三方登录了.效果图如下:
注意:如果你想修改授权的登录页面,可以参考
这个链接里面的第11条
QQ第三方登录实现:
注意
一、我不建议大家去看QQ开放平台关于获取个人信息那部分的文档,因为他们的文档已经过时了,我当时按照文档做过,但是一直报错,后来问客服,他告诉我他们的文档它久远了,已经不能用了<当时我就醉了、、>,而是给了我一份Demo,我会把Demo放在里面供大家下载。
二、大家使用的测试机一定要是自动获取的网络时间,否则得不到信息,并且也没有错误信息,我研究了半天的时间,也没找到到底为什么,一问客服才知道,服务器时间和请求时间戳要相同才可以<我又是醉了、、、>。
三、大家要打包APP再进行测试。
QQ第三方登录的步骤分为:
1.先登录成功获取token和openid
2.再通过token和openid获取用户的信息。
一、导入SDK的jar文件
把这两个jar文件导入项目中
二、配置AndroidManifest
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>
在scheme这个字段输入你申请的appid,tencent不要删掉。
三、添加代码
首先在程序入口添加:
<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><ul class="pre-numbering" style=""><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>
剩下的调用loginQQ方法便可以:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li><li>82</li><li>83</li><li>84</li><li>85</li><li>86</li><li>87</li><li>88</li><li>89</li><li>90</li><li>91</li><li>92</li><li>93</li><li>94</li><li>95</li><li>96</li><li>97</li><li>98</li><li>99</li><li>100</li><li>101</li><li>102</li><li>103</li><li>104</li><li>105</li><li>106</li><li>107</li><li>108</li><li>109</li><li>110</li><li>111</li><li>112</li><li>113</li><li>114</li><li>115</li><li>116</li><li>117</li><li>118</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li><li>82</li><li>83</li><li>84</li><li>85</li><li>86</li><li>87</li><li>88</li><li>89</li><li>90</li><li>91</li><li>92</li><li>93</li><li>94</li><li>95</li><li>96</li><li>97</li><li>98</li><li>99</li><li>100</li><li>101</li><li>102</li><li>103</li><li>104</li><li>105</li><li>106</li><li>107</li><li>108</li><li>109</li><li>110</li><li>111</li><li>112</li><li>113</li><li>114</li><li>115</li><li>116</li><li>117</li><li>118</li></ul>
代码有注释,大家看一下应该很容易明白。
QQ可以获得以下相关信息:
大家只要response.getString(“字段名”)就可以得到数据了:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul>
最后退出登录:
<code class="hljs cs has-numbering">mTencent.logout(<span class="hljs-keyword">this</span>);</code><ul class="pre-numbering" style=""><li>1</li></ul><ul class="pre-numbering" style=""><li>1</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,例如:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>
三、添加代码
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li></ul>
首先在onCreate()方法里面调用regToWx()方法实现微信平台的注册;
其次第一步获取code
调用getCode()方法发送获取code的请求,接受code则是在WXEntryActivity这个类里面的onResp():
第二步使用AsyncTask获取token:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li></ul>
第三步获取用户信息:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li></ul>
微信第三方登录就结束了,相对来说不算太复杂:
注:测试微信登录的时候,也需要打包测试,否则得不到数据;
百度第三方登录
百度第三方登录,相对来说就比较简单了
第一步:导入Baidu-Frontia-Full-Debug-2.0.6.jar包
第二步:配置AndroidManifest.xml
添加android:name=”com.baidu.frontia.ForntiaApplication”
第三步:添加代码
在程序入口初始化Fronta”
<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><ul class="pre-numbering" style=""><li>1</li></ul><ul class="pre-numbering" style=""><li>1</li></ul>
注册百度:
<code class="hljs fix has-numbering"><span class="hljs-attribute">//注册百度 FrontiaAuthorization mAuthorization </span>=<span class="hljs-string"> Frontia.getAuthorization();</span></code><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li></ul>
主要代码直接调用baiduLogin()方法就可以:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li></ul>
退出登录调用baiduExit()方法就可以:
<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><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul><ul class="pre-numbering" style=""><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</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