将curl转换为guzzle给出401错误

时间:2017-05-24 06:41:23

标签: php curl guzzle

我试图将卷曲请求转换为Guzzle。在卷曲中,我得到了响应,就像在Guzzle中一样,它给出了错误。

我的卷曲请求是

        ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode($client_id . ":" . $client_secret), 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded'));
        $url = '<toke endpoint>';
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            "grant_type=authorization_code&code=".$code."&redirect_uri=https://redirect-uri&scope=openid");

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        print_r($result);
        die;

对于guzzle我已将其转换为

 $response = $client->post('<token end point>',[
            'headers' => [
                'Authorization' => "Basic " . base64_encode($client_id . ":" . $client_secret),
                'Accept' => 'application/json',
                'Content-Type' => 'application/x-www-form-urlencoded',
            ], [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'code' => $code,
                'redirect_uri' => '<redirect-uri>',
                'scope' => 'openid',
            ]]
        ]);

        print_r($response);
        echo '<pre>' . var_export($response->getStatusCode(), true) . '</pre>';
        die;

我得到的错误是{"error_description":"CWOAU0025E: The grant_type parameter was invalid: unknown","error":"unsupported_grant_type"}

我过去常常使用curl,然后我在CURLOPT_POSTFIELDS中将其作为url传递。在guzzle中似乎没有用。

0 个答案:

没有答案