ShareSDK第三方平台使用指南——新浪微博

创建应用

1.登录新浪微博控制台并通过认证

2.选择微链接 > 移动应用

3.点击立即接入

4.配置应用信息

以下为创建过程示例,图中信息仅为示例,创建时请按照真实信息填写,否则无法正常使用。

image-20240712175728632

image-20240712175914185

image-20240718105011322

image-20240718105158012

image-20240718105538904

集成使用

获取配置

客户端在使用抖音平台之前,需要先获取新浪微博为应用分发的App Key、App Secret、UniversalLink、授权回调页。

image-20240718110136896

image-20240718110046523

image-20240718105538904

Android

SDK集成

在集成新浪微博相关能力之前,请先按照ShareSDK集成指南完成ShareSDK的集成

appKey的值对应新浪微博平台的App Key,appSecret的值对应新浪微博平台的App Secret,callbackUri的值对应新浪微博平台的授权回调页

MobSDK {
    appKey "xxxxxxxxxxxxx"
    appSecret "xxxxxxxxxxxxxxxx"
    ShareSDK {
        devInfo {
            SinaWeibo {
                appKey "xxxxxxxxxxxxxxxx"
                appSecret "xxxxxxxxxxxxxxxxx"
                callbackUri "xxxxxxxxxxxxxxxxxx"
                enable true
            }  
        }
    }
}

分享示例

代码

以下代码示例分享图片和文字至微博动态

Platform.ShareParams shareParams = new Platform.ShareParams();
shareParams.setImageUrl("http://downhdlogo.yy.com/hdlogo/640640/640/640/44/2258443953/u2258443953a4XSB8M.jpg");
shareParams.setText("分享测试");
Platform platform  = ShareSDK.getPlatform(SinaWeibo.NAME);
platform.setPlatformActionListener(new PlatformActionListener() {
     public void onError(Platform platform, int arg1, Throwable throwable) {
         //失败的回调,platform:平台对象,arg1:表示当前的动作,throwable:异常信息
         Log.d("ShareSDK", "onError ---->  分享失败"+throwable.toString());
     }
     public void onComplete(Platform platform, int arg1, HashMap arg2) {
         //分享成功的回调
         Log.d("ShareSDK", "onComplete ---->  分享成功");
     }
     public void onCancel(Platform platform, int arg1) {
         //取消分享的回调
         Log.d("ShareSDK", "onCancel ---->  取消分享");
     }
});
platform.share(shareParams);
效果图

image-20240718104518717

授权示例

代码
Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
platform.setPlatformActionListener(new PlatformActionListener() {
    @Override
    public void onError(Platform platform, int arg1, Throwable throwable) {
        // TODO 返回授权失败错误码
        Log.e("ShareSDK","==========================="+throwable.toString());
    }
    @Override
    public void onComplete(Platform platform, int arg1, HashMap<String, Object> hashMap)  {
        // TODO 授权成功后获取信息操作等
        Log.e("ShareSDK","=========================="+ platform.getDb().exportData());
    }
    @Override
    public void onCancel(Platform platform, int arg1) {
         // TODO 取消回到
    }
});
platform.showUser(null);
返回信息示例

在ShareSDK成功回调中调用 platform.getDb().exportData(),将返回以下信息

{
    "resume": "",
    "favouriteCount": "57",
    "gender": "1",
    "icon": "https://tvax1.sinaimg.cn/default/images/xxxxxxxxxxxx",
    "snsregat": "1584676272000",
    "secret": "",
    "snsUserUrl": "http://weibo.com/u/xxxxxxxxx",
    "userID": "xxxxxxxxxxx",
    "expiresTime": 1720663247669,
    "token": "x.xxxxxxxxxxxxxxxxxxx",
    "expiresIn": 2653151000,
    "refresh_token": "x.xxxxxxxxxxxxxxxxxxxxxxx",
    "shareCount": "3",
    "secretType": "0",
    "nickname": "xxxxxxxxxxxxxxxxxx",
    "followerCount": "1"
}

iOS

SDK集成

在集成微博相关能力之前,请先按照ShareSDK集成指南完成ShareSDK的集成。

配置URL Scheme

打开项目的Info选项,然后选择URL Types,添加对应平台的URL Scheme配置,微博配置是字符wb+appkey,如下图:

image-20240718100246498

配置白名单

在项目的info.plist中添加Queried URL Schemes,类型为Array,然后添加一个需要支持的项目,类型为字符串类型,添加:sinaweibo,sinaweibohd,sinaweibosso,sinaweibohdsso,weibosdk,weibosdk2.5,weibosdk3.3

10

选择Target,点击Capability,选择Associated Domains,并双击添加,然后点击“+”号,添加Universal link,添加形式为:applinks:XXX,XXX为您Universal link的域名,如下图

image-20240718104254754

在对应AppDelegate.m文件中,添加如下微博初始化代码,Appkey对应微博开放平台中的App Key,appSecret对应微博开放平台中的App Secret,redirectUrl对应微博开放平台中的授权回调页,universalLink对应微博开放平台中的UniversalLink。

[platformsRegister setupSinaWeiboWithAppkey:@"XXXXX" appSecret:@"XXXXXXX" redirectUrl: @"XXXXXX" universalLink:@"XXXXXXX"];

分享示例

代码

以下代码示例分享图片和文字至微博动态

NSMutableDictionary * shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"Jshuoh "
        images:@"https://resource.gy.cn/system/pdf.png"
        url:[NSURL URLWithString:@"http://www.mob.com"]
        title:@"I'm Not The"
         type:SSDKContentTypeImage];                               
[ShareSDK share:SSDKPlatformTypeSinaWeibo 
     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(@"取消");
    }
}];  
效果图

image-20240718104352036

授权示例

代码
[ShareSDK authorize:SSDKPlatformTypeSinaWeibo
           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返回信息为:

{
    "allow_all_act_msg" = 0;
    "allow_all_comment" = 1;
    "audio_ability" = 0;
    "auth_career" = "<null>";
    "auth_career_name" = "<null>";
    "auth_realname" = "<null>";
    "auth_status" = 1;
    "avatar_hd" = "https://XXXXXXXX/default/images/default_avatar_male_180.gif?KID=imgbed,tva&Expires=1720700141&ssig=%2BoER6mSIkh";
    "avatar_large" = "https://XXXXXXXX/default/images/default_avatar_male_180.gif?KID=imgbed,tva&Expires=1720700141&ssig=%2BoER6mSIkh";
    "avatar_type" = 0;
    "bi_followers_count" = 0;
    block = 0;
    "block_app" = 0;
    "block_me" = 0;
    "block_word" = 0;
    "brand_ability" = 0;
    "brand_account" = 0;
    "chaohua_ability" = 0;
    city = 1000;
    class = 1;
    "created_at" = "Thu Oct 17 20:10:13 +0800 2019";
    "credit_score" = 80;
    description = "";
    domain = "";
    "ecommerce_ability" = 0;
    "favourites_count" = 0;
    "follow_me" = 0;
    "followers_count" = 1;
    "followers_count_str" = 1;
    following = 0;
    "friends_count" = 58;
    gender = m;
    "geo_enabled" = 1;
    "gongyi_ability" = 0;
    "green_mode" = 0;
    "hardfan_ability" = 0;
    hongbaofei = 0;
    id = 7321737887;
    idstr = 7321737887;
    insecurity =     {
        "sexual_content" = 0;
    };
    "interaction_user" = 0;
    "is_auth" = 0;
    "is_big" = 0;
    "is_guardian" = 0;
    "is_punish" = 0;
    "is_teenager" = 0;
    "is_teenager_list" = 0;
    lang = "zh-cn";
    "light_ring" = 0;
    like = 0;
    "like_display" = 0;
    "like_me" = 0;
    "live_ability" = 0;
    "live_status" = 0;
    location = "\U5317\U4eac";
    "mb_expire_time" = 0;
    mbrank = 0;
    mbtype = 0;
    name = "\U7528\U62371314212490";
    "newbrand_ability" = 0;
    "nft_ability" = 0;
    "online_status" = 0;
    "pagefriends_count" = 0;
    "paycolumn_ability" = 0;
    "pc_new" = 0;
    "place_ability" = 0;
    "planet_video" = 0;
    "profile_image_url" = "https://XXXXXXX/default/images/default_avatar_male_50.gif?KID=imgbed,tva&Expires=1720700141&ssig=05cKh%2BrjKu";
    "profile_url" = "u/7321737887";
    province = 11;
    ptype = 0;
    remark = "";
    "screen_name" = "\U7528\U62371314212490";
    "show_auth" = 0;
    "special_follow" = 0;
    star = 0;
    status =     {
        "ad_marked" = 0;
        annotations =         (
                        {
                "photo_sub_type" = 0;
            },
                        {
                "client_mblogid" = "iPhone-XXXXXX-1A3D-4915-B8B6-499845497E0A";
            },
                        {
                "phone_id" = "";
                "source_text" = "";
            },
                        {
                "mapi_request" = 1;
            }
        );
        "attitudes_count" = 0;
        "biz_feature" = XXXXXX;
        "bmiddle_pic" = "http://XXXXXX/bmiddle/007Zvguzgy1hrk7qex5a7j305m03i0sm.jpg";
        "can_edit" = 0;

        "can_reprint" = 0;
        "comment_manage_info" =         {
            "ai_play_picture_type" = 0;
            "approval_comment_type" = 0;
            "comment_permission_type" = "-1";
            "comment_sort_type" = 0;
        };
        "comments_count" = 0;
        "content_auth" = 0;
        "created_at" = "Thu Jul 11 17:12:33 +0800 2024";
        "darwin_tags" =         (
        );
        favorited = 0;
        geo = "<null>";
        "gif_ids" = "";
        hasActionTypeCard = 0;
        "hide_flag" = 0;
        "hot_page" =         {
            "feed_detail_type" = 0;
            fid = "232532_mblog";
        };
        "hot_weibo_tags" =         (
        );
        idstr = XXXXXXX;
        "in_reply_to_screen_name" = "";
        "in_reply_to_status_id" = "";
        "in_reply_to_user_id" = "";
        isLongText = 0;
        "is_paid" = 0;
        "is_show_bulletin" = 2;
        "is_show_mixed" = 0;
        "item_category" = status;
        "mblog_vip_type" = 0;
        mblogtype = 0;
        mid = XXXXXXXXX;
        "mixed_count" = 0;
        mlevel = 0;
        "more_info_type" = 0;
        "new_comment_style" = 0;
        "original_pic" = "http://XXXXXX/large/007Zvguzgy1hrk7qex5a7j305m03i0sm.jpg";
        "pending_approval_count" = 0;
        "pic_num" = 1;
        "pic_urls" =         (
                        {
                "thumbnail_pic" = "http://XXXXXXX/thumbnail/007Zvguzgy1hrk7qex5a7j305m03i0sm.jpg";
            }
        );
        "positive_recom_flag" = 0;
        "reposts_count" = 0;
        "reprint_cmt_count" = 0;
        "reward_exhibition_type" = 0;
        rid = 0;
        "show_additional_indication" = 0;
        "show_mlevel" = 0;

        source = "<a href=\"http://weibo.com/\" rel=\"nofollow\">iPhone\U5ba2\U6237\U7aef</a>";
        "source_allowclick" = 0;
        "source_type" = 2;
        text = Jshuoh;
        textLength = 6;
        "thumbnail_pic" = "http://XXXXXX/thumbnail/007Zvguzgy1hrk7qex5a7j305m03i0sm.jpg";
        truncated = 0;
        userType = 0;
        version = 1;
        visible =         {
            "list_id" = 0;
            type = 0;
        };
    };
    "status_total_counter" =     {

        "comment_cnt" = 0;
        "comment_like_cnt" = 0;
        "like_cnt" = 2;
        "repost_cnt" = 0;
        "total_cnt" = 2;
    };
    "statuses_count" = 4;
    "story_read_state" = "-1";
    "super_topic_not_syn_count" = 0;
    svip = 0;
    "unfollowing_recom_switch" = 1;
    urank = 0;
    urisk = 524288;
    url = "";
    "user_ability" = 0;
    "user_ability_extend" = 0;
    "vclub_member" = 0;
    verified = 0;
    "verified_reason" = "";
    "verified_reason_url" = "";
    "verified_source" = "";
    "verified_source_url" = "";
    "verified_trade" = "";
    "verified_type" = "-1";
    "video_mark" = 0;
    "video_play_count" = 0;
    "video_status_count" = 0;
    "video_total_counter" =     {
        "play_cnt" = "-1";
    };
    "vplus_ability" = 0;
    vvip = 0;
    "wbcolumn_ability" = 0;
    weihao = "";
    "wenda_ability" = 0;
}

[user.credential rawData]返回信息为:

{
    "access_token" = "2.XXXXXXXXXXXXX";
    "expires_in" = "1723316399768.691";
    "refresh_token" = "2.XXXXXXXXXXXX";
    uid = XXXXXXXX;
}
1 2 3 4 5 $sharesdk.shareContent($sharesdk.platformID.SinaWeibo, params, function (platform, state, shareInfo, error) { alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error); }); 一键分享 1 2 3 4 5 $sharesdk.oneKeyShareContent([$sharesdk.platformID.SinaWeibo, $sharesdk.platformID.TencentWeibo], params, function (platform, state, shareInfo, error) { alert("state = " + state + "\nshareInfo = " + shareInfo + "\nerror = " + error); }); 关于具体的分享参数名称,请参考“不同平台分享内容的详细说明”章节的说明。 用户授权 调用ShareSDK中的authorize方法可进行用户授权。代码如下: 1 2 3 4 5 $sharesdk.authorize($sharesdk.platformID.SinaWeibo, function (platform, state, error){ alert("state = " + state + "\nerror = " + error); }); 获取用户资料 调用ShareSDK中的getUserInfo来获取授权用户的信息。代码如下: 1 2 3 4 5 $sharesdk.getUserInfo($sharesdk.platformID.SinaWeibo, function (platform, state, user, error) { alert("state = " + state + "\nuser = " + user + "\nerror = " + error); }); iOS部分集成步骤(不发布iOS版本可略过此部分) 导入JS桥接的iOS版本组件 打开下载的JS包里的iOS/ShareSDKForJS/ShareSDKForJS.xcodeproj项目工程,选择ShareSDKForJS静态库运行Xcode,如图: 700px-js_doc_001.png 运行成功之后,请大家打开文件找到Build文件夹,然后进入文件中找到build/Products/Debug-iphonesimulato /libShareSDKForJS.a文件以及找到build/Products/Debug-iphonesimulator/usr/local /include/ShareSDKJSBridge.h这个文件,再将libShareSDKForJS.a和ShareSDKJSBridge.h这 2个文件导入到你的项目里,如图 700px-js_doc_002.png 注意:(如果在分享中弹不出分享的菜单,如果大家的ShareSDK.js文件是本地的,要把这个加到Build Phases里面的Copy Bundle Resources选项) 下载iOS版本ShareSDK,并导入到工程中。 登录ShareSDK的官方网站下载并解压最新版本的SDK。如果您还尚未下载,请 点击这里下载 或者访问 http://sharesdk.cn/Download 。解压后如图所示: 700px-js_doc_003 将ShareSDK导入工程中,并添加相应的依赖库。此步骤可以参考:《iOS快速集成指南》中的第一、第二步。执行完成后,如下图所示: 700px-js_doc_004.png 为UIWebView添加消息捕获 先导入ShareSDKJSBridge.h头文件。 1 #import "ShareSDKJSBridge.h" 然后给UIWebView设置一个委托对象,如: 1 webView.delegate = self; 注意:webView是UIWebView的对象实例。 最后实现shouldStartLoadWithRequest委托方法,并在方法中加入JSBridge的captureRequest方法,代码如下: 1 2 3 4 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { return ![[ShareSDKJSBridge sharedBridge] captureRequest:request webView:webView]; } 添加完毕后就可以对工程进行运行测试。 Android部分集成步骤(不发布Android版本可略过此部分) 导入的步骤如下: 1、参考”安卓快速集成文档“章节的说明,导入ShareSDK for Android的jar库和图片、文字资源。 2、复制github上的jsdemo项目中”cn.sharesdk.js”包中的源代码复制到你的项目中。QQ截图20150304142013 3、在您WebView加载页面地址之前。调用ShareSDKUtils.prepare(webview, webviewClient); 请注意,ShareSDKUtils.prepare方法要在WebView.setWebViewClient(WebViewClinet);之后调用,而且此后不能再调用setWebViewClient方法,否则ShareSDK的初始化操作可能失败。 下面的代码来自github上的demo: Objective-C 1 2 3 4 5 6 7 8 9 10 11 12 public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WebView wvBody = new WebView(this); WebViewClient wvClient = new WebViewClient(); wvBody.setWebViewClient(wvClient); wvBody.setWebChromeClient(new WebChromeClient() { public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MobTech袤博科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值