我通过 OAuth2 URL (https://discordapp.com/api/oauth2/authorize?client_id=398437519408103444&permissions=59392&scope=bot
) 验证我的机器人
我想在我的 discord 服务器上发送消息到 channel
代码:
$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
最佳答案
事实证明,您只需要使用身份验证 token 连接到网关一次,从那时起它应该允许通过 api 发送消息。我使用链接中的 html 文件来完成此操作,从此一切顺利。
https://github.com/restcord/restcord/blob/master/extra/gateway.html
导致我到这里的原始 github 问题在这里 https://github.com/restcord/restcord/issues/68
关于php - Discord API 错误 #40001 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48096094/