使用Discord API卷曲PUT请求导致错误的请求

时间:2020-07-09 04:18:30

标签: php curl oauth-2.0 discord

我正在使用php,oauth2和curl连接到Discord API,并将成员添加到服务器(行会)。

我已经成功使用Oath2对identity,guilds和guilds.join范围的用户进行了身份验证。 我有所需的access_token和漫游器令牌。

根据我在Discord API文档中的理解: https://discord.com/developers/docs/resources/guild#add-guild-member

我需要使用带有access_token(具有guilds.join范围)和带有Authentication:Bot Token的标头的PUT请求到端点:https://discordapp.com/api/guilds/GUILDID/members/USERID

我已完成此操作,但收到400错误的请求错误。 我不确定是什么问题。

配置

define('OAUTH2_CLIENT_ID', 'REDACTED');
define('OAUTH2_CLIENT_SECRET', 'REDACTED');

$guildid = REDACTED

$authorizeURL = 'https://discord.com/api/oauth2/authorize?client_id=REDACTED&permissions=0&redirect_uri=https%3A%2F%2FREDACTED%2Findex.php%3Faction%3Ddiscord&response_type=code&scope=identify%20guilds%20guilds.join%20bot';
$tokenURL = 'https://discordapp.com/api/oauth2/token';
$apiURLBase = 'https://discordapp.com/api/users/@me';
$apiURLGuilds = 'https://discordapp.com/api/users/@me/guilds';
$apiURLJoin = 'https://discordapp.com/api/guilds/'. $guildid . '/members/';

加入功能

function joinapiRequest($url,$userid) {
  $url = $url.$userid;

  $params = 'access_token='.session('access_token');
  


  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

  $bottoken = redacted;
  $headers[] = 'Authorization: Bot ' . $bottoken;
  $headers[] = 'Content-Type: application/json';
  $headers[] = 'Content-Length: '.strlen($params);
  
  
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  $response = curl_exec($ch);

  echo 'join guild response <br />';
  var_dump($response);
  

  if($http == 201){
    return true;
  } else {
    return false;
  }


}

响应

string(42) "{"message": "400: Bad Request", "code": 0}" 

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

对于任何寻求答案的人,我通过更改将access_token发送至以下语法来解决此问题:

$params = '{"access_token" : "'.session('access_token').'"}';