ShareSDK实现第三方登录和分享

来源:互联网 发布:网盘搜索 知乎 编辑:程序博客网 时间:2024/05/01 21:31

使用ShareSDK实现第三方登录和分享

  • 1、创建项目
  • 2、在shareSDK网站上下载相关的SDK并解压
  • 3、找开ShareSDK for Android文件夹,双击QuickIntegrater.jar文件,选择需要集成的第三方软件,确定后形成Sample文件夹,将此文件夹下的所有文件复制到自己的项目中(覆盖,注意,如出现红色叉号,可找开对应项目检查文件是否出现乱码,出现乱码更改编码格式即可)。
  • 4、assets文件下ShareSDK.xml进行相应配置(需要在ShareSDK官网上生成appkey,和第三方平台上注册相应的AppKey)。
    如下:
这里写代码片<?xml version="1.0" encoding="utf-8"?><DevInfor><!--说明:1、表格中的第一项<ShareSDK AppKey="api20" />是必须的,其中的AppKey是您在ShareSDK上注册的开发者帐号的AppKey2、所有集成到您项目的平台都应该为其在表格中填写相对应的开发者信息,以新浪微博为例:<SinaWeibo Id="1" SortId="1" AppKey="568898243" AppSecret="38a4f8204cc784f81f9f0daaf31e02e3" RedirectUrl="http://www.mob.com" Enable="true" />其中的SortId是此平台在分享列表中的位置,由开发者自行定义,可以是任何整型数字,数值越大越靠后AppKey、AppSecret和RedirectUrl是您在新浪微博上注册开发者信息和应用后得到的信息Id是一个保留的识别符,整型,ShareSDK不使用此字段,供您在自己的项目中当作平台的识别符。Enable字段表示此平台是否有效,布尔值,默认为true,如果Enable为false,即便平台的jar包已经添加到应用中,平台实例依然不可获取。各个平台注册应用信息的地址如下:新浪微博        http://open.weibo.com腾讯微博        http://dev.t.qq.comQQ空间          http://connect.qq.com/intro/login/微信好友        http://open.weixin.qq.comFacebook       https://developers.facebook.comTwitter        https://dev.twitter.com人人网          http://dev.renren.com开心网          http://open.kaixin001.com搜狐微博        http://open.t.sohu.com网易微博        http://open.t.163.com豆瓣           http://developers.douban.com有道云笔记      http://note.youdao.com/open/developguide.html#app印象笔记        https://dev.evernote.com/Linkedin       https://developer.linkedin.comFourSquare     https://developer.foursquare.com/搜狐随身看      https://open.sohu.com/Flickr         http://www.flickr.com/services/Pinterest      http://developers.pinterest.com/Tumblr         http://www.tumblr.com/developersDropbox        https://www.dropbox.com/developersInstagram      http://instagram.com/developer#VKontakte      http://vk.com/dev易信好友        http://open.yixin.im/明道             http://open.mingdao.com/Line           http://media.line.me/zh-hant/Pocket         http://getpocket.com/developer/apps/newKakaoTalk      https://developers.kakao.com/KakaoStory     https://developers.kakao.com/--><ShareSDK AppKey = "13e094dfa4350"/> <!-- 修改成你在sharesdk后台注册的应用的appkey"--><!-- ShareByAppClient标识是否使用微博客户端分享,默认是false --><SinaWeiboId="1"SortId="1"AppKey="568898243"AppSecret="38a4f8204cc784f81f9f0daaf31e02e3"RedirectUrl="http://www.sharesdk.cn"ShareByAppClient="true"Enable="true" /><TencentWeiboId="2" SortId="2"AppKey="801307650"AppSecret="ae36f4ee3946e1cbb98d6965b0b2ff5c"RedirectUri="http://sharesdk.cn"Enable="true" /><QZoneId="3"SortId="3"AppId="1105471814"AppKey="0PeCRCXKeraeYAnu"Enable="true" /><!-- Wechat微信和WechatMoments微信朋友圈的appid是一样的;注意:开发者不能用我们这两个平台的appid,否则分享不了微信测试的时候,微信测试需要先签名打包出apk,sample测试微信,要先签名打包,keystore在sample项目中,密码123456BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过审核的应用也可以执行分享,但是仅限于分享文字和图片,不能分享其他类型,默认值为false。此外,微信收藏不支持此字段。--><WechatId="4"SortId="4"AppId="wx4868b35061f87885"AppSecret="64020361b8ec4c99936c0e3999a9f249"BypassApproval="false"Enable="true" /><WechatMomentsId="5"SortId="5"AppId="wx4868b35061f87885"AppSecret="64020361b8ec4c99936c0e3999a9f249"BypassApproval="false"Enable="true" /><WechatFavoriteId="6"SortId="6"AppId="wx4868b35061f87885"AppSecret="64020361b8ec4c99936c0e3999a9f249"Enable="true" /><!-- ShareByAppClient标识是否使用微博客户端分享,默认是false --><QQId="7"SortId="7"AppId="100371282"AppKey="0PeCRCXKeraeYAnu"ShareByAppClient="true"Enable="true" /><FacebookId="8"SortId="8"ConsumerKey="107704292745179"ConsumerSecret="38053202e1a5fe26c80c753071f0b573"RedirectUrl="http://mob.com"Enable="true" /><TwitterId="9"SortId="9"ConsumerKey="LRBM0H75rWrU9gNHvlEAA2aOy"ConsumerSecret="gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"CallbackUrl="http://mob.com"Enable="true" /><RenrenId="10"SortId="10"AppId="226427"ApiKey="fc5b8aed373c4c27a05b712acba0f8c3"SecretKey="f29df781abdd4f49beca5a2194676ca4"Enable="true" /><KaiXinId="11"SortId="11"AppKey="358443394194887cee81ff5890870c7c"AppSecret="da32179d859c016169f66d90b6db2a23"RedirectUri="http://www.sharesdk.cn"Enable="true" /><EmailId="12" SortId="12"Enable="true" /><ShortMessageId="13" SortId="13"Enable="true" /><DoubanId="16"SortId="16"ApiKey="031a96a3aa8b28af094fc3eaffa17a0d"Secret="2e675e730571b75d"RedirectUri="http://mob.com"Enable="true" /><YouDaoId="17"SortId="17"HostType="product"ConsumerKey="dcde25dca105bcc36884ed4534dab940"ConsumerSecret="d98217b4020e7f1874263795f44838fe"RedirectUri="http://www.sharesdk.cn/"Enable="true" /><!-- 在中国大陆,印象笔记有两个服务器,一个是沙箱(sandbox),一个是生产服务器(china)。一般你注册应用,它会先让你使用sandbox,当你完成测试以后,可以到http://dev.yinxiang.com/support/上激活你的ConsumerKey,激活成功后,修改HostType为china就好了。至于如果您申请的是国际版的印象笔记(Evernote),则其生产服务器类型为“product”。如果目标设备上已经安装了印象笔记客户端,ShareSDK允许应用调用本地API来完成分享,但是需要将应用信息中的“ShareByAppClient”设置为true,此字段默认值为false--><EvernoteId="19"SortId="19"HostType="sandbox"ConsumerKey="sharesdk-7807"ConsumerSecret="d05bf86993836004"ShareByAppClient="true"Enable="true" /><LinkedInId="20"SortId="20"ApiKey="ejo5ibkye3vo"SecretKey="cC7B2jpxITqPLZ5M" RedirectUrl="http://sharesdk.cn"Enable="true" /><GooglePlusId="21"SortId="21"Enable="true" /><FourSquareId="22"SortId="22"ClientID="G0ZI20FM30SJAJTX2RIBGD05QV1NE2KVIM2SPXML2XUJNXEU"ClientSecret="3XHQNSMMHIFBYOLWEPONNV4DOTCDBQH0AEMVGCBG0MZ32XNU"RedirectUrl="http://www.sharesdk.cn"Enable="true" /><PinterestId="23"SortId="23"ClientId="1432928"Enable="true" /><FlickrId="24"SortId="24"ApiKey="33d833ee6b6fca49943363282dd313dd"ApiSecret="3a2c5b42a8fbb8bb"RedirectUri="http://www.sharesdk.cn"Enable="true" /><TumblrId="25"SortId="25"OAuthConsumerKey="2QUXqO9fcgGdtGG1FcvML6ZunIQzAEL8xY6hIaxdJnDti2DYwM"SecretKey="3Rt0sPFj7u2g39mEVB3IBpOzKnM3JnTtxX2bao2JKk4VV1gtNo"CallbackUrl="http://sharesdk.cn"Enable="true" /><DropboxId="26"SortId="26"AppKey="i5vw2mex1zcgjcj" AppSecret="3i9xifsgb4omr0s"RedirectUri="https://www.sharesdk.cn"Enable="true" /><VKontakte Id="27"SortId="27"ApplicationId="3921561"Enable="true" /><Instagram Id="28"SortId="28"ClientId="ff68e3216b4f4f989121aa1c2962d058"ClientSecret="1b2e82f110264869b3505c3fe34e31a1"RedirectUri="http://sharesdk.cn"Enable="true" /><!-- Yixin易信和YixinMoments易信朋友圈的appid是一样的;        注意:开发者不能用我们这两个平台的appid,否则分享不了易信测试的时候需要先签名打包出apk,sample测试易信,要先签名打包,keystore在sample项目中,密码123456BypassApproval是绕过审核的标记,设置为true后AppId将被忽略,故不经过审核的应用也可以执行分享,但是仅限于分享文字或图片,不能分享其他类型,默认值为false--><Yixin Id="29"SortId="29"AppId="yx0d9a9f9088ea44d78680f3274da1765f"BypassApproval="true"Enable="true" /><YixinMomentsId="30"SortId="30"AppId="yx0d9a9f9088ea44d78680f3274da1765f"BypassApproval="true"Enable="true" /><MingdaoId="31"SortId="31"AppKey="EEEE9578D1D431D3215D8C21BF5357E3"AppSecret="5EDE59F37B3EFA8F65EEFB9976A4E933"RedirectUri="http://sharesdk.cn"Enable="true" /><LineId="32"SortId="32"Enable="true" /><KakaoTalkId="33"SortId="33"AppKey="48d3f524e4a636b08d81b3ceb50f1003"Enable="true" /><KakaoStoryId="34"SortId="34"AppKey="48d3f524e4a636b08d81b3ceb50f1003"Enable="true" /><WhatsAppId="35"SortId="35"Enable="true" /><BluetoothId="36"SortId="36"Enable="true" /><PocketId="37"SortId="37"ConsumerKey="32741-389c565043c49947ba7edf05"Enable="true" /><InstapaperId="38"SortId="38"ConsumerKey="4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"ConsumerSecret="GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"Enable="true" /><FacebookMessengerId="39"SortId="39"AppId="107704292745179"Enable="true" /><AlipayId="52"SortId="52"  AppId="2015072400185895"Enable="true"/></DevInfor>
  • 5、在AndroidManifest.xml配置文件中添加权限:
<uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /><uses-permission android:name="android.permission.GET_ACCOUNTS" /><!-- 蓝牙分享所需的权限 --><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />添加activity:<activityandroid:name="com.mob.tools.MobUIShell"android:configChanges="keyboardHidden|orientation|screenSize"android:screenOrientation="portrait"android:theme="@android:style/Theme.Translucent.NoTitleBar"android:windowSoftInputMode="stateHidden|adjustResize" ><intent-filter> <data android:scheme="tencent100371282" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT" /></intent-filter><!-- 调用新浪原生SDK,需要注册的回调activity --><intent-filter> <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity>
  • 6、代码中实现:
    代码实现中需要在启动项oncreat()方法中或application类中初始SDK:
    ShareSDK.initSDK(this);
    分享代码:
    “`
    这里写代码片
    public void share(View v) {

OnekeyShare oks = new OnekeyShare();
// 关闭sso授权
oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字 2.5.9以后的版本不调用此方法
// oks.setNotification(R.drawable.ic_launcher,
// getString(R.string.app_name));
// title标题,印象笔记、邮箱、信息、微信、人人网和QQ空间使用
oks.setTitle(getString(R.string.share));
// titleUrl是标题的网络链接,仅在人人网和QQ空间使用
oks.setTitleUrl(“http://sharesdk.cn“);
// text是分享文本,所有平台都需要这个字段
oks.setText(“个人分享”);
// imagePath是图片的本地路径,Linked-In以外的平台都支持此参数
// oks.setImagePath(“/sdcard/test.jpg”);//确保SDcard下面存在此张图片
// url仅在微信(包括好友和朋友圈)中使用
oks.setUrl(“http://sharesdk.cn“);
// comment是我对这条分享的评论,仅在人人网和QQ空间使用
oks.setComment(“测试一下了”);
// site是分享此内容的网站名称,仅在QQ空间使用
oks.setSite(getString(R.string.app_name));
// siteUrl是分享此内容的网站地址,仅在QQ空间使用
oks.setSiteUrl(“http://sharesdk.cn“);

// 启动分享GUI
oks.show(this);
}

登录代码

public void QLogin(View v){//添加授权Platform platform11 = ShareSDK.getPlatform(QQ.NAME);//platform11.SSOSetting(true);platform11.authorize();platform11.showUser(null);//必须要加的要不然不行!这个才是授权的!tm = (TelephonyManager) MainActivity.this.getSystemService(TELEPHONY_SERVICE); imi = tm.getDeviceId();platform11.setPlatformActionListener(new PlatformActionListener() {@Overridepublic void onError(Platform platform11, int arg1, Throwable arg2) {//弹出失败窗口}@SuppressLint("SimpleDateFormat") @Overridepublic void onComplete(Platform platform11, int arg1, HashMap<String, Object> arg2) {System.out.println("登录成功。。。。");//成功得到用户信息String userId = platform11.getDb().getUserId();String userName = platform11.getDb().getUserName();String token = platform11.getDb().getToken();String userIcon = platform11.getDb().getUserIcon();//有效时期long expiresTime = platform11.getDb().getExpiresTime();SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm");System.out.println("用户ID为:"+userId);System.out.println("用户名称为:"+userName);System.out.println("token     "+token);System.out.println("expiresTime  "+sdf3.format(expiresTime));//HttpUties ht=new HttpUties();//ht.UplodePhotos(1+"", userId+"", userName+"", token+"", expiresTime+"", imi+"");}@Overridepublic void onCancel(Platform arg0, int arg1) {}});}

“`

0 0
原创粉丝点击