接收并处理twitter平台发送的消息接口
接口功能
接收并处理twitter平台发送的消息。
接口鉴权
获取请求头x-twitter-webhooks-signature中的认证字符串,截去前面的字符串"sha256="。
获取渠道配置的appSecret,将请求中request,与appSecret进行sha256加密,生成对比的鉴权字符串。
String signatureHeader = request.getHeader("x-twitter-webhooks-signature");
String payloadContent = IOUtils.toString(request.getReader());
String signature = StringUtils.substringAfter(signatureHeader, "sha256=");
try {
String payloadHash = BaseUtil.hMacSha256(payloadContent, consumerKey);
if (!StringUtils.equals(signature, payloadHash)) {
log.error("The signature is not correct, channelId = {}", channelId);
return false;
}
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
log.error("get payloadHash failed", e);
return false;
}
接口原型
|
请求方法 |
POST |
|
|---|---|---|
|
请求URL |
https://ip:port/social/on/twitter/{channelId} |
其中,ip:port为对外暴露的nslb的ip和port, channelId为渠道id |
请求参数
|
参数名称 |
选取原则 |
参数类型 |
默认值 |
说明 |
|---|---|---|---|---|
|
Content-Type |
必选 |
String |
无 |
固定填 application/x-www-form-urlencoded。 |
|
x-twitter-webhooks-signature |
必选 |
String |
无 |
用于上行消息接口鉴权,调用twitter回调接口生成。 |
|
参数名称 |
选取原则 |
参数类型 |
默认值 |
说明 |
|---|---|---|---|---|
|
direct_message_events |
必选 |
JSONArray |
无 |
|
|
users |
必选 |
String |
无 |
用户相关数据,详见表6 users中的参数说明 |
|
参数名称 |
选取原则 |
参数类型 |
默认值 |
说明 |
|---|---|---|---|---|
|
message_create |
必选 |
JSONObject |
无 |
用户发送的消息,详见表5 message_create中的参数说明 |
|
created_timestamp |
必选 |
String |
无 |
发送消息时间戳。 |
响应参数
当调用本接口响应成功,response 无响应内容。
接口示例
- 请求示例
POST x-twitter-webhooks-signature: xxxxxxxxxxxxxxx /************************* Accept: */* Host: 10.154.198.164 Content-Type: application/x-www-form-urlencoded { "direct_message_events" : [ "message_create" : { "sender_id" : "01122552341412", "message_id" : "aaaswfwefew1255" }, "created_timestamp" : "1111235235666" ], "user" : { "id" : "01122552341412", "name" : "sgyyub", "profile_image_url_https" : "https://www.twitter/xxxxxx" } }