php - Discord API 错误 #40001 未经授权

标签 php discord php-curl

我通过 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/

相关文章:

php - 如何防止 css 文件中的图像被缓存?

python - 不正确的 token 传递

javascript - 从 php cURL 获取正确的编码

ubuntu - curl 错误 : Could not resolve host: domain

php - 更新智能表中的单元格

php - 而在 Mysql 中只提取最后的结果?

php - Docker-为PHP + Nginx安装PDO驱动程序

javascript - 有没有办法找出用户拥有的最高角色?

python - 尝试获取公会角色时出现 Discord API 401 未经授权错误

apache2.4 - 无法加载模块 'ext\\php_curl.dll',因为它与 9.0 链接