Appearance
获取私信 SessionId
获取和某个视频号用户的私信会话 sessionId,之后用它调私信文字或私信图片。
接口地址: POST /getSessionId
说明与注意
toUserName来自搜索、主页、关注列表等视频号接口的返回。sessionId和目标用户、发送身份、登录实例绑定,任一变了要重新获取。
请求参数
| 参数名 | 必选 | 类型 | 说明 |
|---|---|---|---|
| wId | 是 | string | 登录实例标识 |
| toUserName | 是 | string | 目标视频号用户编码 |
| myUserName | 是 | string | 自己的视频号用户编码;type=2 时传空字符串 |
| type | 是 | int | 身份类型:1 视频号身份,2 微信号身份 |
请求示例
http
POST {BASE_URL}/getSessionId
Authorization: Bearer <你的调用凭证>
Content-Type: application/json
{
"wId": "{{wId}}",
"toUserName": "v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder",
"myUserName": "v2_060000231003b20faec8cae18a1ec5d0cb07eab077ba915250774edbea38082ea6b24af229@finder",
"type": 1
}bash
curl -X POST '{BASE_URL}/getSessionId' \
-H 'Authorization: Bearer <你的调用凭证>' \
-H 'Content-Type: application/json' \
--data-raw '{
"wId": "{{wId}}",
"toUserName": "v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder",
"myUserName": "v2_060000231003b20faec8cae18a1ec5d0cb07eab077ba915250774edbea38082ea6b24af229@finder",
"type": 1
}'python
import requests
url = "{BASE_URL}/getSessionId"
headers = {"Authorization": "Bearer <你的调用凭证>", "Content-Type": "application/json"}
body = """{
"wId": "{{wId}}",
"toUserName": "v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder",
"myUserName": "v2_060000231003b20faec8cae18a1ec5d0cb07eab077ba915250774edbea38082ea6b24af229@finder",
"type": 1
}"""
resp = requests.post(url, headers=headers, data=body.encode("utf-8"))
print(resp.text)java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
String url = "{BASE_URL}/getSessionId";
String body = "{\n" +
" \"wId\": \"{{wId}}\",\n" +
" \"toUserName\": \"v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder\",\n" +
" \"myUserName\": \"v2_060000231003b20faec8cae18a1ec5d0cb07eab077ba915250774edbea38082ea6b24af229@finder\",\n" +
" \"type\": 1\n" +
"}";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.header("Authorization", "Bearer <你的调用凭证>")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());php
<?php
$url = '{BASE_URL}/getSessionId';
$body = <<<'JSON'
{
"wId": "{{wId}}",
"toUserName": "v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder",
"myUserName": "v2_060000231003b20faec8cae18a1ec5d0cb07eab077ba915250774edbea38082ea6b24af229@finder",
"type": 1
}
JSON;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer <你的调用凭证>',
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;成功响应
json
{
"code": "1000",
"message": "处理成功",
"data": {
"sessionId": "69fd3a9cc1180847d8b5a1533bf285b99a83784cfd4cfdadea78974509359c74@findermsg",
"enableAction": 0,
"toUsername": "v2_060000231003b20faec8c6e78010c3d4c605eb3cb077f16e37c172145877400390b1170a0299@finder"
}
}错误响应
json
{
"code": "1001",
"message": "失败",
"data": null
}响应参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| code | string | 1000 成功;其他值都是失败,通常是 1001 |
| message | string | 反馈信息 |
| data | object | 会话信息 |
| data.sessionId | string | 私信会话 ID,发送私信时使用 |
| data.enableAction | int | 会话动作状态,按接口返回值处理 |
| data.toUsername | string | 目标视频号用户编码 |