简体   繁体   中英

Need help converting a PHP CURL script to guzzle

Hello I have this working CURL post request I wish to recode with guzzle. But I'm failing on a 400 Bad Request. Anyone see what I'm doing wrong:

 $response = $this->getHttpClient()->post(
        'https://login.eveonline.com/oauth/token', [
        'headers' => [
            'Authorization' => 'Basic  '.base64_encode(
                env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
            )
        ],
        'grant_type' => 'refresh_token',
        'refresh_token' => $refresh_token,
    ]);

    return json_decode($response->getBody()->getContents(), true);

    /*
    $c = curl_init();
    $cver = curl_version();
    $contact = 'postmaster@asite.net';      
    curl_setopt($c, CURLOPT_URL, 'https://login.eveonline.com/oauth/token');
    curl_setopt($c, CURLOPT_USERAGENT, "asite/admin (curl/{$cver['version']}; {$contact})");
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query([
        'grant_type' => 'refresh_token',
        'refresh_token' => $refresh_token,
    ]));
    curl_setopt($c, CURLOPT_HTTPHEADER, [ 'Authorization: Basic '.base64_encode(
        env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
    ) ]);

    $rawjson = curl_exec($c);
    return json_decode($rawjson, true); 
    */

RESPONSE: Client error: POST https://login.eveonline.com/oauth/token resulted in a 400 Bad Request response: {"error":"invalid_request","error_description":"Unknown grant_type"}

Fixed it already sorry for the inconvenience:

 $response = $this->getHttpClient()->post(
        'https://login.eveonline.com/oauth/token', [
        'headers' => [
            'Authorization' => 'Basic  '.base64_encode(
                env('EVEONLINE_CLIENT_ID').':'.env('EVEONLINE_CLIENT_SECRET')
            )
        ],
        'json' => [
            'grant_type' => 'refresh_token',
            'refresh_token' => $refresh_token,
        ]           
    ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM