Jasmin Dreasond
Jasmin Dreasond

Reputation: 11

Problems with Curl in Discord Oauth2

the problem is simple.

when the oAuth2 code is sent to the page. This php script to receive the token json not work. Please, help me :c

$base64 = base64_encode($data['id'] . ':' . $data['secret']);

$info = curl_init();
curl_setopt_array($info, array(
    CURLOPT_URL => "https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=" . $data['code'] . "&redirect_uri=" . urlencode($data['redirect']),
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "Authorization: Basic " . $base64,                                                                                
        'Content-Length: ' . strlen($base64)
    ),
    CURLOPT_RETURNTRANSFER => true
));

$tinyresult = curl_exec($info);
curl_close($info);

return $tinyresult;

Upvotes: 0

Views: 875

Answers (1)

Jasmin Dreasond
Jasmin Dreasond

Reputation: 11

Problem solved :D

    $info = curl_init();

    curl_setopt_array($info, array(
        CURLOPT_URL => "https://discordapp.com/api/oauth2/token",
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(
            "grant_type" => "authorization_code",
            "client_id" => $data['id'],
            "client_secret" => $data['secret'],
            "redirect_uri" => $data['redirect'],
            "code" => $data["code"],
        ),
        CURLOPT_RETURNTRANSFER => true,
    ));

    $tinyresult = curl_exec($info);
    if ($tinyresult == false) {
        $tinyerror = curl_error($info);
    } else {
        $tinyerror = null;
        $tinyresult = json_decode($tinyresult);
    }

    $httpcode = curl_getinfo($info, CURLINFO_HTTP_CODE);

    curl_close($info);

    return array(
        "data" => $tinyresult,
        "err" => $tinyerror,
        "state" => $httpcode,
    );

Upvotes: 1

Related Questions