Laravel HTTP客户端post请求不工作
尝试从Instagram获取访问令牌Laravel HTTP客户端post请求不工作,laravel,guzzle,Laravel,Guzzle,尝试从Instagram获取访问令牌 $params = array( 'app_id' => $this->clientId, 'app_secret' => $this->clientSecret, 'grant_type' => 'authorization_code', 'redirect_uri' => $this->redirectU
$params = array(
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
);
$ch = curl_init();
$endpoint = $this->getBaseUrl() . 'oauth/access_token';
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
curl_close($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
dd($response,$header_size,$header,$body);
$response = json_decode($response, true);
return $response;
这段带有CURL的代码运行良好
Laravel文档
$response = Http::post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->body();
但无法使用Laravel Http客户端。返回的客户端Id丢失,但我将作为参数发送。我猜您的内容类型是
application/x-www-form-urlencoded
,请尝试使用asForm()
有点不清楚你想要实现什么。看起来您几乎在尝试访问您自己的OAuth每个点,对吗?无论如何,您不应该发送
客户端id
而不是应用程序id
,如下所述:客户端id的内容类型是什么request@MaartenVeerman从Instagram获取访问令牌。是的,这是为我做的,干杯
$response = Http::asForm()->post($this->getBaseUrl() . 'oauth/access_token', [
'app_id' => $this->clientId,
'app_secret' => $this->clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUrl,
'code' => $this->request->code
]
);
return $response->json();