首页 文章

Webhook OAuth - 如何为访问令牌交换webhook授权代码?

提问于
浏览
5

我正在尝试将oauth approach of adding webhooks用于Discord中的 Channels . 工作流程是用户使用OAuth对我的应用程序进行身份验证 . 然后我将它们重定向到:

ApiClient::API_URL.'/oauth2/authorize?client_id='.Discord::appKey().'&scope=webhook.incoming&redirect_uri='.urlencode($webhookCallback->callbackUrl()).'&response_type=code');

重定向网址有效,因为它允许OAuth'd用户选择服务器/ Channels .

当您交换访问令牌的授权代码时,令牌响应将包含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"}

完成此请求需要哪种授权类型?

1 回答

  • 4

    'grant_type' => 'authorization_code',

    需要

    'grant_type' => 'client_credentials',

相关问题