消息推送服务

跳转到: 导航, 搜索

目录

消息推送服务概述

当一个和认证用户相关的消息或者事件发生时(如某个用户向认证用户发送消息),微博服务器将POST消息数据包到开发者填写的URL上;


在开发者首次使用事件推送服务时,需要先通过一次校验来和微博服务器建立首次连接;微博服务器发送GET请求到开发者填写的URL上,校验参数如下表所示:

校验参数字段 字段类型 字段说明
signature string 微博加密签名,signature结合了开发者appsecret参数和请求中的timestamp参数,nonce参数
timestamp string 时间戳
nonce string 随机数
echostr string 随机字符串


signature参数的加密规则为:将appsecret参数,timestamp参数,nonce参数进行字典排序后,将三个参数字符串拼接成一个字符串进行sha1加密;开发者收到请求后,首先通过加密后的signature参数来校验GET请求的真实性,如果确认此次GET请求来自微博服务器,原样返回echostr参数内容就可以成功建立首次连接,否则连接失败。


建立首次连接后,后续每次微博事件推送时也都会带上signature、timestamp、nonce三个参数,开发者依然可以通过对signature的校验判断此条消息的真实性。校验方式与首次建立连接一致。

接受消息推送

建立首次连接后,当认证用户有新消息时,微博消息推送服务会POST消息数据包到开发者填写的URL上,当前支持的消息类型:

注意:需要对接收到的消息体type或subtype做兼容,当出现未知类型时可忽略此消息。)

事件消息:event

{
    "type": "event",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "事件消息",
    "data": {
        "subtype": "EVENT",
        "key": "EVENT_KEY",
        "ticket": "TICKET",
    }
}
返回值说明
属性 值的类型 说明描述
type string event
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 默认文案。subtype为follow或unfollow时分别为“关注事件消息”、“取消关注事件消息”;为subscribe或unsubscribe时为触发订阅的私信关键词(如“dy”),非私信触发时(点击订阅按钮)为“订阅事件消息”、“取消订阅事件消息”;subtype为scan或scan_follow时为“扫描二维码”;
data string 消息内容
data:subtype string follow:关注事件,unfollow取消关注事件,subscribe订阅事件,unsubscribe订阅事件。scan和scan_follow为二维码扫描事件。
data:key string subtype为follow、unfollow、subscribe或unsubscribe时不返回
data:ticket string subtype为scan和scan_follow时才返回


纯文本类型私信和留言消息:text

{
    "type": "text",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "私信或留言内容",
    "data": {}
}
返回值说明
属性 值的类型 说明描述
type string text
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 私信内容
data string 消息内容,纯文本私信或留言为空


位置类型私信消息:position

{
    "type": "position",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "我在这里: http://t.cn/zQgLLYO",
    "data": {
        "longitude": "116.308586",
        "latitude": "39.982525"
      }
}
返回值说明
属性 值的类型 说明描述
type string mention
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 原位置私信文本,没有时用默认文案“发送了一个位置”
data string 消息内容
data:longitude string 经度
data:latitude string 纬度


语音类型私信和留言消息:voice

{
    "id": 1211260020031347,
    "type": "voice",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "发了一个语音消息",
    "data": {
        "vfid": 821804459,    // 发送者用此ID查看语音
        "tovfid": 821804469  // 接收者用此ID查看语音
    }
}
返回值说明
属性 值的类型 说明描述
type string voice
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 私信内容
data string 消息内容,纯文本私信或留言为空
data:vfid string 语音文件ID,发送者通过此ID读取语音
data:tovfid string 语音文件ID,接收者通过此ID读取语音
查看(下载)认证用户接收到的语音方法一
curl "https://upload.api.weibo.com/2/mss/msget?access_token=RECIPIENT_ACCESS_TOKEN&fid=TOVFID"

1,"RECIPIENT_ACCESS_TOKEN":返回结果中接收者(recipient_id)通过OAuth2授权返回的access_token;
2,"TOVFID":返回结果data字段中的tovfid。

查看(下载)认证用户接收到的语音方法二
curl -u "USERNAME:PASSWORD" "https://upload.api.weibo.com/2/mss/msget?source=APPKEY&fid=TOVFID"

1,"USERNAME:PASSWORD"为"recipient_id"(认证用户)的微博登录用户名和密码,此时方法二中的APPKEY应用所有者为"recipient_id";
2,"TOVFID":返回结果data字段中的tovfid。


图片类型私信和留言消息:image

{
    "type": "image",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "发了一张图片",
    "data": {
        "vfid": 821804459,     // 发送者用此ID查看图片
        "tovfid": 821804469    // 接收者用此ID查看图片
    }
}
返回值说明
属性 值的类型 说明描述
type string image
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 私信内容
data string 消息内容,纯文本私信或留言为空
data:vfid string 图片ID,发送者通过此ID读取图片
data:tovfid string 图片ID,接收者通过此ID读取图片
查看(下载)图片
curl "https://upload.api.weibo.com/2/mss/msget?access_token=RECIPIENT_ACCESS_TOKEN&fid=TOVFID"

1,"RECIPIENT_ACCESS_TOKEN":返回结果中接收者(receiver_id)通过OAuth2授权返回的access_token;
2,"TOVFID":返回结果data字段中的tovfid。


被@消息:mention

说明:指定的认证用户需被授予接收“被@消息”权限,此接口才返回“被@消息”,申请可邮件 open_api@sina.com

{
    "type": "mention",
    "receiver_id": 1902538057,
    "sender_id": 2489518277,
    "created_at": "Mon Jul 16 18:09:20 +0800 2012",
    "text": "被@的微博或评论文本信息",
    "data": {
        "subtype": "MENTION_TYPE,
        "key": "MENTION_KEY"
    }
}

// 默认仅返回可信用户的@,如需返回所有用户@,认证用户可访问此链接设置:http://account.weibo.com/set/message
返回值说明
属性 值的类型 说明描述
type string mention
receiver_id int64 消息的接收者
sender_id int64 消息的发送者
created_at string 消息创建时间
text string 被@的微博或评论文本信息
data string 消息内容
data:subtype string status:@的微博,comment:@的评论
data:key string 当subtype为status时为微博ID,为comment时为评论ID


发送被动响应

应用的创建及发布包括以下流程。 注:开发者在平台创建应用时可选择应用适用终端,开发时对相应地址进行适配即可在各终端iframe中展示。


(1) 成为开发者

填写开发者信息,成为开发者:http://open.weibo.com/developers/basicinfo

通过审核成为开发者后,即可开发Page应用。


XML格式兼容模式

主要是BD合作引入,步骤为:

(1) 商务沟通

    • - 双方沟通产品结合点
    • - 确立合作意向


(2) 产品沟通

    • - 确认结合点在产品、技术层面可实现
    • - 确认产品表现方式


(3) 应用开发

    • - 快速完成线上申请
    • - 根据文档和规范进行开发


(4) 应用上线及推广

    • - 应用审核通过上线
    • - 确认双方推广资源及推广方式