2
votes

Edit: I'm wondering if I am adding my bot wrong? The bot is on the same application as the user's OAuth 2. The bot was added with: https://discordapp.com/oauth2/authorize?client_id={my id}&scope=bot&permissions=267906503

I have an OAuth2 connection with Discord using https://github.com/wohali/oauth2-discord-new This returns the discord user and my access token. It has scopes for:

   $authUrl = $this->provider->getAuthorizationUrl([
      'scope' => [
        'identify',
        'email',
        'guilds.join']
      ]);

Using the same discord application, I have a bot set up. The bot is a member of the guild, and the bot is responsive to chat pings (meaning, it's working). The bot has the create instant invite permission.

Right after I get the discord user back, and have just generated the access token, I am trying to add that user to my guild.

I have tried this with the restcord library and then directly with guzzle.

    $guzzle = new Client(['base_uri' => 'https://discordapp.com/api/v6']);

    $guildId = $this->discordApplication->guildId();
    $request = new Request('PUT', 'https://discordapp.com/api/v6/guilds/'.$guildId.'/members/'.$user->discordId(), [
      'Authorization' => 'Bot '.$this->discordApplication->accessToken()->__toString(),
      "Content-Type" => "application/json"
    ] );
    var_dump($request);
    $response = $guzzle->send($request);
    var_dump($response);

It is generating a 401 error. I've looked here and here to make sure my content type was right, that my authorization header was right, my OAuth scope was right, and my bot permissions were right.

The output is:

object(GuzzleHttp\Psr7\Request)#82 (7) {
  ["method":"GuzzleHttp\Psr7\Request":private]=>
  string(3) "PUT"
  ["requestTarget":"GuzzleHttp\Psr7\Request":private]=>
  NULL
  ["uri":"GuzzleHttp\Psr7\Request":private]=>
  object(GuzzleHttp\Psr7\Uri)#83 (7) {
    ["scheme":"GuzzleHttp\Psr7\Uri":private]=>
    string(5) "https"
    ["userInfo":"GuzzleHttp\Psr7\Uri":private]=>
    string(0) ""
    ["host":"GuzzleHttp\Psr7\Uri":private]=>
    string(14) "discordapp.com"
    ["port":"GuzzleHttp\Psr7\Uri":private]=>
    NULL
    ["path":"GuzzleHttp\Psr7\Uri":private]=>
    string(60) "/api/v6/guilds/{right guild id}/members/{the right user id}"
    ["query":"GuzzleHttp\Psr7\Uri":private]=>
    string(0) ""
    ["fragment":"GuzzleHttp\Psr7\Uri":private]=>
    string(0) ""
  }
  ["headers":"GuzzleHttp\Psr7\Request":private]=>
  array(3) {
    ["Host"]=>
    array(1) {
      [0]=>
      string(14) "discordapp.com"
    }
    ["Authorization"]=>
    array(1) {
      [0]=>
      string(34) "Bot {my oauth2 bearer access token}"
    }
    ["Content-Type"]=>
    array(1) {
      [0]=>
      string(16) "application/json"
    }
  }
  ["headerNames":"GuzzleHttp\Psr7\Request":private]=>
  array(3) {
    ["authorization"]=>
    string(13) "Authorization"
    ["content-type"]=>
    string(12) "Content-Type"
    ["host"]=>
    string(4) "Host"
  }
  ["protocol":"GuzzleHttp\Psr7\Request":private]=>
  string(3) "1.1"
  ["stream":"GuzzleHttp\Psr7\Request":private]=>
  NULL
}
Client error: `PUT https://discordapp.com/api/v6/guilds/{guildid}/members/{userid}` resulted in a `401 UNAUTHORIZED` response:
{"code": 0, "message": "401: Unauthorized"}

1

1 Answers

2
votes

I figured it out.

   $request = new Request(
      'PUT', 'https://discordapp.com/api/v6/guilds/'.$guildId.'/members/'.$user->discordId(),
      [
        'Authorization' => 'Bot '.$this->botToken,
        "Content-Type" => "application/json"
      ],
      '{"access_token" : "'. $this->discordApplication->accessToken()->__toString().'"}'
      );