1

I have problem with my code, the error I get is:

Warning: fopen(https://discordapp.com/api/v6/auth/login): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in C:\xampp\htdocs\s2.php on line 15

Here is the relevant Code:

<?php
$data = array("email" => 'Email@gmail.com', "password" => 'Password');
$data_string = json_encode($data);                                                                                   
$context_options = array (
        'https' => array (
            'method' => 'POST',
            'header'=> "Content-type: application/json\r\n"
                . "Content-Length: " . strlen($data_string). "\r\n",
                 'content' => $data_string

            )
        );

$context = stream_context_create($context_options);
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);

?>

Thank you for your help!

SaschaM78
  • 4,376
  • 4
  • 33
  • 42
M G C C
  • 45
  • 1
  • 7

1 Answers1

1

It seems directly logging in via a bot is no longer allowed as you should use the OAuth2 (how does OAuth2 work?) functionality of Discord. This means that your bot needs to be set up inside your Discord account and then you may use token-based access on the bot to authenticate the external application against Discord. The change to no longer allow bot-based logins happened around the beginning of 2017 and at that point all PHP-based Discord-related Github applications stopped to be maintained. Here is a discussion and comment about banning bots with automated login and this one about that OAuth2 has to be used.

Read more about authentication and bots in the Discord OAuth2 chapter.

If you can tell more about what you plan to achieve, maybe we can help you find a solution to your task.


Previous (no-longer working) answer:

I haven't used DiscordApp so I haven't tried this yet. Instead of sending JSON-encoded data, have you tried posting the values as FORM values to the API?

$postData = array(
  'email' => 'Email@gmail.com', 
  'password' => 'Password'
);
$params = array('https' =>
  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postData
  )
);

$context  = stream_context_create($params);

// get the entire response ...
$result = file_get_contents('https://discordapp.com/api/v6/auth/login', false, $context);

// ... or alternatively using fopen()
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);

I haven't found any docs regarding how the parameters should be passed to the login URI but at least that's how normal form-based logins can be used.

According to the DiscordApp Response codes a 400 may be received if either the call was not understood (non-existent function called) or the parameters were send improperly so you should find out about how to call the logininterface with parameters using scripts.

SaschaM78
  • 4,376
  • 4
  • 33
  • 42