【发布时间】:2023-04-06 04:00:01
【问题描述】:
我正在尝试将 oauth approach of adding webhooks 用于 Discord 中的频道。工作流程是用户使用 OAuth 对我的应用程序进行身份验证。然后我将它们重定向到:
ApiClient::API_URL.'/oauth2/authorize?client_id='.Discord::appKey().'&scope=webhook.incoming&redirect_uri='.urlencode($webhookCallback->callbackUrl()).'&response_type=code');
重定向 URL 有效,因为它允许 OAuth 用户选择服务器/通道。
当您用授权码交换访问令牌时,令牌响应将包含 webhook 对象:
我正在使用以下请求尝试将授权码转换为访问令牌,但没有成功:
$client = new Client();
$response = $client->post('https://discordapp.com/api/oauth2/token', [
'headers' => [
'Accept' => 'application/json'
],
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => env('DISCORD_APP_KEY'),
'client_secret' => env('DISCORD_APP_SECRET'),
'redirect_uri' => url('/discord/webhook-authorized'),
'code' => $request->get('code')
],
]);
我从 API 得到的响应是:
Client error: `POST https://discordapp.com/api/oauth2/token` resulted in a `401 UNAUTHORIZED` response:
{"error": "access_denied"}
我需要什么类型的授权来完成这个请求?
【问题讨论】:
-
这实际上并不意味着您输入了错误的
grant_type,而是意味着您的代码无效或您的重定向URI 与代码不匹配。 (见tools.ietf.org/html/rfc6749#section-5.2) -
@PeterG 这是有道理的。我访问的 URL 与 oauth 使用的 URL 相同,因此我可能需要访问不同的 URL。文档对此不是很清楚。
标签: discord