创建应用
1.登录Twitter控制台并通过认证
2.点击Developer Portal进入Twitter后台
3.点击Sign up for Free Account创建应用
4.配置应用信息
以下为创建过程示例,图中信息仅为示例,创建时请按照真实信息填写,否则无法正常使用。
权限申请
Twitter分享和授权需要申请权限,以及配置回调地址
集成使用
获取配置
客户端在使用Twitter平台之前,需要先获取Twitter为应用分发的Client ID、Client Secret、Callback URI / Redirect URL。
Android
SDK集成
在集成Twitter相关能力之前,请先按照ShareSDK集成指南完成ShareSDK的集成
appKey的值对应Twitter平台的Client ID,appSecret的值对应Twitter平台的Client Secret,callbackUri的值对应Twitter平台的Callback URI / Redirect URL
MobSDK {
appKey "xxxxxxxxxxxxx"
appSecret "xxxxxxxxxxxxxxxx"
ShareSDK {
devInfo {
Twitter {
appKey "xxxxxxxxxxxxxxxx"
appSecret "xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx"
callbackUri "xxxxxxxxxxxxxxxx"
IsUseV2 true
shareByAppClient true
bypassApproval true
enable true
}
}
}
}
分享示例
代码
以下代码示例分享图片和文字至Twitter
Platform.ShareParams shareParams = new Platform.ShareParams();
shareParams.setText("测试文本");
shareParams.setImageUrl("http://pic28.photophoto.cn/20130818/0020033143720852_b.jpg");
Platform platform = ShareSDK.getPlatform(Twitter.NAME);
//设置分享事件回调(注:回调放在不能保证在主线程调用,不可以在里面直接处理UI操作)
platform.setPlatformActionListener(new PlatformActionListener() {
public void onError(Platform platform, int arg1, Throwable throwable) {
//失败的回调,arg:平台对象,arg1:表示当前的动作,arg2:异常信息
Log.d("ShareSDK", "onError ----> 分享失败" + throwable.toString());
}
public void onComplete(Platform platform, int arg1, HashMap hashMap) {
//分享成功的回调
Log.d("ShareSDK", "onComplete ----> 分享成功");
}
public void onCancel(Platform platform, int arg1) {
//取消分享的回调
Log.d("ShareSDK", "onCancel ----> 取消分享");
}
});
platform.share(shareParams);
效果图
授权示例
代码
Platform platform = ShareSDK.getPlatform(Twitter.NAME);
platform.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onError(Platform platform, int arg1, Throwable throwable) {
// TODO 返回授权失败错误码
arg2.printStackTrace();
Log.e("ShareSDK","===========================登录失败"+throwable.toString());
}
@Override
public void onComplete(Platform platform, int arg1, HashMap<String, Object> hashMap) {
// TODO 授权成功后获取信息操作等
Log.e("ShareSDK","=========================="+platform.getDb().exportData());
Log.e("ShareSDK","=========================="+hashMap.toString());
}
@Override
public void onCancel(Platform platform, int arg1) {
// TODO 取消回调
Log.e("ShareSDK","===========================登录取消");
}
});
platform.showUser(null);
返回信息示例
在ShareSDK成功回调中调用 platform.getDb().exportData()
,将返回以下信息
{
"resume": "null",
"favouriteCount": "null",
"gender": "2",
"icon": "null",
"snsregat": "0",
"snsUserUrl": "https://twitter.com/null",
"userID": "0",
"token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"shareCount": "null",
"secretType": "0",
"nickname": "null",
"name": "xxx",
"id": "xxxxxxxxxxxxxxx",
"followerCount": "null",
"username": "xxxxxxxxxxxxxxxxx"
}
iOS
SDK集成
在集成微博相关能力之前,请先按照ShareSDK集成指南完成ShareSDK的集成。
配置URL Scheme
打开项目的Info选项,然后选择URL Types,添加URL Scheme配置 twitterkit-Twitter的ClientID
,例如:twitterkit-xxxxxxxxxxxxxxx
配置白名单
(1)在项目的info.plist中添加Queried URL Schemes,类型为Array
(2)添加一个需要支持的项目,类型为字符串类型,twitter需要添加的白名单为twitterauth
初始化SDK
在使用Twitter能力之前,需要先对其初始化,其中setupTwitterWithKey的值对应Twitter平台中的Client ID,secret对应Twitter平台中的Client Secret,redirectUrl对应Twitter平台中的Callback URI / Redirect URL
[ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
[platformsRegister setupTwitterWithKey:@"XXXXXXXXXXX" secret:@"XXXXXXXXXXXXXXXX" redirectUrl:@"XXXXXXXXXXXX"];
}
分享示例
代码
以下代码示例分享图片至Twitter
#import <ShareSDK/ShareSDK.h>
NSMutableDictionary * shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"Jshuoh "
images:@"http://download.sdk.mob.com/web/images/2019/07/30/14/1564468183056/750_750_65.12.png"
url: nil
title:@"I'm Not The"
type:SSDKContentTypeImage];
[ShareSDK share:SSDKPlatformTypeTwitter parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
if (state == SSDKResponseStateSuccess)
{
NSLog(@"分享成功");
}
if (state == SSDKResponseStateFail)
{
NSLog(@"失败----%@",error.description);
}
if (state == SSDKResponseStateCancel)
{
NSLog(@"取消");
}
}];
效果图
授权示例
代码
[ShareSDK authorize:SSDKPlatformTypeTwitter
settings:nil
onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
if (state == SSDKResponseStateSuccess)
{
NSLog(@"rawData---%@",user.rawData);//用户信息打印
NSLog(@"credential---%@",[user.credential rawData]);//授权信息打印
}
if (state == SSDKResponseStateFail)
{
NSLog(@"%@",error.description);
}
if (state == SSDKResponseStateCancel)
{
NSLog(@"取消");;
}
}];
返回信息示例
user.rawData返回信息为:
{
id = XXXXXXX;
name = XXXXXXX;
username = XXXXXX;
}
[user.credential rawData]返回信息为:
{
"access_token" = XXXXXXXXXXXXXX;
"expires_in" = 7200;
scope = "tweet.write users.read tweet.read";
"token_type" = bearer;