Discord API错误#40001未经授权

问题描述 投票:1回答:1

我通过OAuth2 URL(https://discordapp.com/api/oauth2/authorize?client_id=398437519408103444&permissions=59392&scope=bot)验证我的机器人

我想在我的discord服务器上发送消息到频道

码:

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, 'https://discordapp.com/api/channels/'.$channel_id.'/messages');
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    // 'Content-Type: application/json',
    'Authorization: Bot '.$this->token
]);
curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode([
    'content' => 'test'
]));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($curl);
curl_close ($curl);
var_dump(json_decode($server_output, true));

但回应是:

array(2) {
  ["code"]=>
  int(40001)
  ["message"]=>
  string(12) "Unauthorized"
}

API链接:https://discordapp.com/developers/docs/resources/channel#create-message

php discord php-curl
1个回答
-1
投票

POST /信道/ {channel.id} /消息

在使用此端点之前,您必须至少连接一次网关并与网关一起识别。

将消息发布到公会文本或DM频道。如果在公会频道上操作,此端点需要在当前用户上出现“SEND_MESSAGES”权限。

您的机器人是否在该频道中拥有'SEND_MESSAGES'权限?

没有?很好地转到频道设置并授予其权限。

您的机器人是否与网关至少连接并识别?

没有?那么你应该去做那件事,最好使用已经制作的library

如果尝试上述解决方案,这个问题仍然存在,那么你可能会以某种方式错误地发布你的POST请求。

© www.soinside.com 2019 - 2024. All rights reserved.