第2行: | 第2行: | ||
<div class="wiki_txtJ"> | <div class="wiki_txtJ"> | ||
− | JS SDK 2020 | + | JS SDK 2020 是微博开放平台最新版的、针对移动端网页的SDK,它较之前版本有较大改变: |
<ul class="wiki_indent_list"> | <ul class="wiki_indent_list"> | ||
第51行: | 第51行: | ||
<script src="https://open.weibo.com/views/js/wbsdk.js" type="text/javascript" charset="utf-8"></script> | <script src="https://open.weibo.com/views/js/wbsdk.js" type="text/javascript" charset="utf-8"></script> | ||
</pre> | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | 支持使用 AMD/CMD 标准模块加载方法加载。 | ||
+ | </div> | ||
第77行: | 第81行: | ||
}); | }); | ||
</pre> | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | 其中,配置参数,debug:开启调试模式,appkey:应用唯一标识,timestamp:生成签名的时间戳,noncestr:生成签名的随机串,signature:签名,scope:需要使用的JS接口列表。 | ||
+ | </div> | ||
<div style="margin:15px 0;"> | <div style="margin:15px 0;"> | ||
第92行: | 第100行: | ||
<div style="margin:15px 0;"> | <div style="margin:15px 0;"> | ||
签名算法见文档后面的说明,JS 接口的说明见后面。 | 签名算法见文档后面的说明,JS 接口的说明见后面。 | ||
+ | </div> | ||
+ | |||
+ | |||
+ | <div class="wiki_title_withline">4、通过 ready 接口处理成功验证</div> | ||
+ | |||
+ | <div class="code_type">JS</div> | ||
+ | <pre class="brush:js"> | ||
+ | wb.ready(function () { | ||
+ | alert("## init success"); | ||
+ | }); | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | init 初始化后会执行 ready 方法,所有JS 接口调用都必须在 ready 接口获得结果之后,init 是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在 ready 函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在 ready 函数中。 | ||
+ | </div> | ||
+ | |||
+ | |||
+ | <div class="wiki_title_withline">5、通过 error 接口处理失败验证</div> | ||
+ | |||
+ | <div class="code_type">JS</div> | ||
+ | <pre class="brush:js"> | ||
+ | wb.error(function (res) { | ||
+ | alert("## init error: " + res); | ||
+ | }); | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | init 初始化失败会执行 error 函数,如签名过期导致验证失败,错误信息可以在返回的 res 参数中查看,可以在这里更新签名,重新尝试初始化。 | ||
</div> | </div> | ||
第112行: | 第148行: | ||
</pre> | </pre> | ||
− | <div | + | <div style="margin:15px 0;">开放标签中的样式,完全由开发者自己控制,例如。</div> |
<div class="code_type">HTML</div> | <div class="code_type">HTML</div> | ||
第119行: | 第155行: | ||
</pre> | </pre> | ||
− | <div | + | <div style="margin:15px 0;">指定分享的内容和页面地址,可以通过 data:title,data:url 属性标签完成自定义,例如。</div> |
<div class="code_type">HTML</div> | <div class="code_type">HTML</div> | ||
第126行: | 第162行: | ||
</pre> | </pre> | ||
− | <div | + | <div style="margin:15px 0;">如果需要在分享的时候,指定一张分享图片,需要先将图片上传到微博图床,然后再开放标签上指定图片的PID,例如。</div> |
<div class="code_type">HTML</div> | <div class="code_type">HTML</div> | ||
第145行: | 第181行: | ||
</pre> | </pre> | ||
− | <div | + | <div style="margin:15px 0;">其中,data:keyword 属性为需要指定的关键词,data:height 属性为显示 view 的高度。</div> |
第259行: | 第295行: | ||
!width="20%"|类型 | !width="20%"|类型 | ||
!width="60%"|说明描述 | !width="60%"|说明描述 | ||
+ | |- | ||
+ | |icon | ||
+ | |String | ||
+ | |分享到微信的卡片图标的图片地址。 | ||
+ | |- | ||
+ | |title | ||
+ | |String | ||
+ | |分享到微信的卡片标题。 | ||
+ | |- | ||
+ | |desc | ||
+ | |String | ||
+ | |分享到微信的卡片描述。 | ||
|- | |- | ||
|success | |success | ||
第270行: | 第318行: | ||
− | <div class="wiki_title_withline"></div> | + | <div class="wiki_title_withline">wb.openMenu</div> |
− | <div class="wiki_txtJ"></div> | + | <div class="wiki_txtJ">打开分享菜单</div> |
<div style="margin:15px 0;">'''示例'''</div> | <div style="margin:15px 0;">'''示例'''</div> | ||
第278行: | 第326行: | ||
<div class="code_type">Javascript</div> | <div class="code_type">Javascript</div> | ||
<pre class="brush:js"> | <pre class="brush:js"> | ||
− | + | wb.openMenu({ | |
+ | success: function (res) { | ||
+ | alert("## openMenu success: " + JSON.stringify(res)); | ||
+ | }, | ||
+ | fail: function (res) { | ||
+ | alert("## openMenu fail: " + JSON.stringify(res)); | ||
+ | } | ||
+ | }); | ||
</pre> | </pre> | ||
<div style="margin:15px 0;">'''参数说明'''</div> | <div style="margin:15px 0;">'''参数说明'''</div> | ||
− | |||
{|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | {|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | ||
第300行: | 第354行: | ||
− | <div class="wiki_title_withline"></div> | + | <div class="wiki_title_withline">wb.shareToWxTimeline</div> |
− | <div class="wiki_txtJ"></div> | + | <div class="wiki_txtJ">分享到微信朋友圈</div> |
<div style="margin:15px 0;">'''示例'''</div> | <div style="margin:15px 0;">'''示例'''</div> | ||
第308行: | 第362行: | ||
<div class="code_type">Javascript</div> | <div class="code_type">Javascript</div> | ||
<pre class="brush:js"> | <pre class="brush:js"> | ||
− | + | wb.shareToWxTimeline({ | |
+ | success: function (res) { | ||
+ | alert("## shareToWxTimeline success: " + JSON.stringify(res)); | ||
+ | }, | ||
+ | fail: function (res) { | ||
+ | alert("## shareToWxTimeline fail: " + JSON.stringify(res)); | ||
+ | } | ||
+ | }); | ||
</pre> | </pre> | ||
<div style="margin:15px 0;">'''参数说明'''</div> | <div style="margin:15px 0;">'''参数说明'''</div> | ||
− | |||
{|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | {|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | ||
第330行: | 第390行: | ||
− | <div class="wiki_title_withline"></div> | + | <div class="wiki_title_withline">wb.shareToWxMessage</div> |
− | <div class="wiki_txtJ"></div> | + | <div class="wiki_txtJ">分享到微信好友</div> |
<div style="margin:15px 0;">'''示例'''</div> | <div style="margin:15px 0;">'''示例'''</div> | ||
第338行: | 第398行: | ||
<div class="code_type">Javascript</div> | <div class="code_type">Javascript</div> | ||
<pre class="brush:js"> | <pre class="brush:js"> | ||
+ | wb.shareToWxMessage({ | ||
+ | success: function (res) { | ||
+ | alert("## shareToWxMessage success: " + JSON.stringify(res)); | ||
+ | }, | ||
+ | fail: function (res) { | ||
+ | alert("## shareToWxMessage fail: " + JSON.stringify(res)); | ||
+ | } | ||
+ | }); | ||
+ | </pre> | ||
+ | <div style="margin:15px 0;">'''参数说明'''</div> | ||
+ | |||
+ | {|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | ||
+ | <html><colgroup></colgroup></html> | ||
+ | !width="20%"|参数名称 | ||
+ | !width="20%"|类型 | ||
+ | !width="60%"|说明描述 | ||
+ | |- | ||
+ | |success | ||
+ | |Function | ||
+ | |调用成功后的回调函数。 | ||
+ | |- | ||
+ | |fail | ||
+ | |Function | ||
+ | |调用失败后的回调函数。 | ||
+ | |} | ||
+ | |||
+ | |||
+ | <div class="wiki_title_withline">wb.scanQRCode</div> | ||
+ | |||
+ | <div class="wiki_txtJ">扫描二维码</div> | ||
+ | |||
+ | <div style="margin:15px 0;">'''示例'''</div> | ||
+ | |||
+ | <div class="code_type">Javascript</div> | ||
+ | <pre class="brush:js"> | ||
+ | wb.scanQRCode({ | ||
+ | success: function (res) { | ||
+ | alert("## scanQRCode success: " + JSON.stringify(res)); | ||
+ | }, | ||
+ | fail: function (res) { | ||
+ | alert("## scanQRCode fail: " + JSON.stringify(res)); | ||
+ | } | ||
+ | }); | ||
</pre> | </pre> | ||
<div style="margin:15px 0;">'''参数说明'''</div> | <div style="margin:15px 0;">'''参数说明'''</div> | ||
+ | {|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | ||
+ | <html><colgroup></colgroup></html> | ||
+ | !width="20%"|参数名称 | ||
+ | !width="20%"|类型 | ||
+ | !width="60%"|说明描述 | ||
+ | |- | ||
+ | |success | ||
+ | |Function | ||
+ | |调用成功后的回调函数。 | ||
+ | |- | ||
+ | |fail | ||
+ | |Function | ||
+ | |调用失败后的回调函数。 | ||
+ | |} | ||
+ | |||
+ | |||
+ | <div class="wiki_title_withline">wb.pickContact</div> | ||
+ | |||
+ | <div class="wiki_txtJ">选择微博好友</div> | ||
+ | |||
+ | <div style="margin:15px 0;">'''示例'''</div> | ||
+ | |||
+ | <div class="code_type">Javascript</div> | ||
+ | <pre class="brush:js"> | ||
+ | wb.pickContact({ | ||
+ | count: 3, | ||
+ | success: function (res) { | ||
+ | alert("## pickContact success: " + JSON.stringify(res)); | ||
+ | }, | ||
+ | fail: function (res) { | ||
+ | alert("## pickContact fail: " + JSON.stringify(res)); | ||
+ | } | ||
+ | }); | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;">'''参数说明'''</div> | ||
{|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | {|width="100%" border="0" cellspacing="0" cellpadding="0" class="wiki_table" | ||
第349行: | 第488行: | ||
!width="20%"|类型 | !width="20%"|类型 | ||
!width="60%"|说明描述 | !width="60%"|说明描述 | ||
+ | |- | ||
+ | |count | ||
+ | |Int | ||
+ | |可以选择的人数,例如,传1就是只能选一个人,最大不能超过10。 | ||
|- | |- | ||
|success | |success | ||
第363行: | 第506行: | ||
==JS SDK 2020 使用权限签名算法== | ==JS SDK 2020 使用权限签名算法== | ||
− | <div class="wiki_txtJ"> | + | <div class="wiki_txtJ">生成签名之前必须先了解一下 jsapi_ticket,jsapi_ticket 是网页用于调用微博客户端内JS接口的临时票据。正常情况下,jsapi_ticket 的有效期为7200秒。由于有效期内再次调用该接口会导致 ticket 刷新,旧的直接失效。因此频繁刷新 jsapi_ticket 有可能导致签名校验失败,影响自身业务,因此,建议开发者在自己的服务全局缓存 jsapi_ticket 。</div> |
+ | |||
+ | <div class="wiki_title_withline">获取 jsapi_ticket 的接口,POST请求</div> | ||
+ | |||
+ | <div class="code_type">API</div> | ||
+ | <pre class="brush:html"> | ||
+ | https://api.weibo.com/oauth2/js_ticket/generate?client_id=APPKEY&client_secret=APPSECRET | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;">'''返回值'''</div> | ||
+ | |||
+ | <div class="code_type">JSON</div> | ||
+ | <pre class="brush:js"> | ||
+ | { | ||
+ | "result": true, | ||
+ | "appkey": "", | ||
+ | "js_ticket": "", | ||
+ | "expire_time": 7199 | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;">其中,js_ticket 为需要获取的 jsapi_ticket ,expire_time 为过期时间。</div> | ||
+ | |||
+ | <div class="wiki_title_withline">签名算法</div> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | 签名生成规则如下:参与签名的字段包括 noncestr,有效的 jsapi_ticket,timestamp,url。对所有待签名参数按照字段名的 ASCII 码从小到大排序(字典序)后,使用 key1=value1&key2=value2… 的格式拼接成字符串。这里需要注意的是所有参数名均为小写字符,之后对字符串进行 sha1 加密,字段名和字段值都采用原始值,其中,url 为当前网页的完整地址,但是不包含 # 及其后面部分,也不要进行 URL 转义。 | ||
+ | </div> | ||
+ | |||
+ | <div class="code_type">签名字符串</div> | ||
+ | <pre class="brush:js"> | ||
+ | 签名字符串 = "jsapi_ticket={jsapi_ticket}&noncestr={noncestr}×tamp={timestamp}&url={url}" | ||
+ | </pre> | ||
+ | |||
+ | <div class="code_type">签名</div> | ||
+ | <pre class="brush:js"> | ||
+ | 签名 = sha1(签名字符串) | ||
+ | </pre> | ||
+ | |||
+ | <div style="margin:15px 0;"> | ||
+ | 签名用的 noncestr 和 timestamp 必须与 init 中的 noncestr 和 timestamp 相同。签名用的 url 必须是调用JS接口页面的完整地址。出于安全考虑,开发者必须在服务器端实现签名的逻辑。 | ||
+ | </div> | ||
2020年12月23日 (三) 17:57的版本
JS SDK 2020
JS SDK 2020 是微博开放平台最新版的、针对移动端网页的SDK,它较之前版本有较大改变:
- 1、聚焦移动端H5场景,去除了原来PC时代的旧组件;
- 2、分享功能,适配多场景,无论是在微博、微信、手机浏览器,还是PC上,都可以方便使用;
- 3、采用开放标签,样式完全由开发者自己控制;
- 4、更加安全,因此去除了原来的JS前端直接授权、直接调接口的功能,防止数据外泄,相应的页面授权需求,需要开发者按照标准授权流程接入,调用开放接口则需要放在服务端完成;
下面我们来一起体验一下这些特性。
开发指南
如果你没有AppKey请先在开放平台网站,注册自己的应用,申请入口。
在使用JS SDK 2020时,需要为该应用绑定安全域名,其中微博客户端JS 接口将以此域名来进行校验。
绑定安全域名的操作可以在 我的应用 的 高级信息 页面完成。
在你的页面部署 wbsdk.js 文件,同时,如果你的页面编码不是UTF-8,请添加charset="utf-8"属性。
<script src="https://open.weibo.com/views/js/wbsdk.js" type="text/javascript" charset="utf-8"></script>
支持使用 AMD/CMD 标准模块加载方法加载。
通过 wb.init 接口注入权限验证配置,所有需要使用微博客户端内JS SDK 的页面必须先注入配置信息,否则将无法调用。
wb.init({ debug: false, appkey: '', timestamp: , noncestr: '', signature: '', scope: [ 'getNetworkType', 'setBrowserTitle', 'setSharingContent', 'openMenu', 'scanQRCode', 'pickContact' ] });
其中,配置参数,debug:开启调试模式,appkey:应用唯一标识,timestamp:生成签名的时间戳,noncestr:生成签名的随机串,signature:签名,scope:需要使用的JS接口列表。
如果你的应用处于开发调试过程,建议使用JS SDK 2020的调试模式,可以在初始化时,配置 debug 为 true,开启调试模式,此时在调用出错时,出错信息将通过 alert 弹出,方便在移动端调试。
wb.init({ debug: false, ... });
签名算法见文档后面的说明,JS 接口的说明见后面。
wb.ready(function () { alert("## init success"); });
init 初始化后会执行 ready 方法,所有JS 接口调用都必须在 ready 接口获得结果之后,init 是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在 ready 函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在 ready 函数中。
wb.error(function (res) { alert("## init error: " + res); });
init 初始化失败会执行 error 函数,如签名过期导致验证失败,错误信息可以在返回的 res 参数中查看,可以在这里更新签名,重新尝试初始化。
开放标签
<wb-share-button>分享到微博</wb-share-button>
<wb-share-button><div class="myStyle">分享到微博</div></wb-share-button>
<wb-share-button data:title="title" data:url="url">分享到微博</wb-share-button>
<wb-share-button data:pid="53b515f0ly1glgemvpjwfj20gi0aw114">分享到微博</wb-share-button>
<wb-topic-timeline data:keyword="王者荣耀" data:height="500"></wb-topic-timeline>
微博客户端内JS 接口说明
wb.getNetworkType({ success: function (res) { alert("## getNetworkType success: " + JSON.stringify(res)); }, fail: function (res) { alert("## getNetworkType fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.setBrowserTitle({ title: "JS SDK DEMO", success: function (res) { alert("## setBrowserTitle success: " + JSON.stringify(res)); }, fail: function (res) { alert("## setBrowserTitle fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
title | String | 需要设置的页面标题。 |
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.setSharingContent({ icon: "https://img.t.sinajs.cn/t6/style/images/face/face_card_longwb.png", title: "SDK Sharing Content", desc: "SDK Sharing Content", success: function (res) { alert("## setSharingContent success: " + JSON.stringify(res)); }, fail: function (res) { alert("## setSharingContent fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
icon | String | 分享到微信的卡片图标的图片地址。 |
title | String | 分享到微信的卡片标题。 |
desc | String | 分享到微信的卡片描述。 |
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.openMenu({ success: function (res) { alert("## openMenu success: " + JSON.stringify(res)); }, fail: function (res) { alert("## openMenu fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.shareToWxTimeline({ success: function (res) { alert("## shareToWxTimeline success: " + JSON.stringify(res)); }, fail: function (res) { alert("## shareToWxTimeline fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.shareToWxMessage({ success: function (res) { alert("## shareToWxMessage success: " + JSON.stringify(res)); }, fail: function (res) { alert("## shareToWxMessage fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.scanQRCode({ success: function (res) { alert("## scanQRCode success: " + JSON.stringify(res)); }, fail: function (res) { alert("## scanQRCode fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
wb.pickContact({ count: 3, success: function (res) { alert("## pickContact success: " + JSON.stringify(res)); }, fail: function (res) { alert("## pickContact fail: " + JSON.stringify(res)); } });
参数名称 | 类型 | 说明描述 |
---|---|---|
count | Int | 可以选择的人数,例如,传1就是只能选一个人,最大不能超过10。 |
success | Function | 调用成功后的回调函数。 |
fail | Function | 调用失败后的回调函数。 |
JS SDK 2020 使用权限签名算法
https://api.weibo.com/oauth2/js_ticket/generate?client_id=APPKEY&client_secret=APPSECRET
{ "result": true, "appkey": "", "js_ticket": "", "expire_time": 7199 }
签名生成规则如下:参与签名的字段包括 noncestr,有效的 jsapi_ticket,timestamp,url。对所有待签名参数按照字段名的 ASCII 码从小到大排序(字典序)后,使用 key1=value1&key2=value2… 的格式拼接成字符串。这里需要注意的是所有参数名均为小写字符,之后对字符串进行 sha1 加密,字段名和字段值都采用原始值,其中,url 为当前网页的完整地址,但是不包含 # 及其后面部分,也不要进行 URL 转义。
签名字符串 = "jsapi_ticket={jsapi_ticket}&noncestr={noncestr}×tamp={timestamp}&url={url}"
签名 = sha1(签名字符串)
签名用的 noncestr 和 timestamp 必须与 init 中的 noncestr 和 timestamp 相同。签名用的 url 必须是调用JS接口页面的完整地址。出于安全考虑,开发者必须在服务器端实现签名的逻辑。