我正在向github o auth发送请求以获取用户的访问令牌并尝试使用guzzle但是响应没有任何价值我不知道为什么,在另一个手,当我使用curl它工作正常并成功返回访问令牌, 我正在使用guzzle中的form_parms来设置字段
这里是guzzle示例
public function fetchGithubToken(\Illuminate\Http\Request $request, $code)
{
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://github.com/login/oauth/access_token', [
'form_params' => ['client_id' => env('GITHUB_CLIENTID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'code' => $code],
]);
Log::error(print_r($response, true));
return $response;
}
这里是卷曲的例子
public function post_to_url($url, $data)
{
$fields = '';
foreach ($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
return $result;
}
public function fetchGithubToken(\Illuminate\Http\Request $request, $code)
{
$data = ['client_id' => env('GITHUB_CLIENTID'), 'client_secret' => env('GITHUB_CLIENT_SECRET'), 'code' => $code];
$response = $this->post_to_url("https://github.com/login/oauth/access_token", $data);
Log::info(print_r($response, true));
return $response;
}
答案 0 :(得分:0)
您必须在cURL中添加身份验证。您必须使用以下语句,因为github需要身份验证:
Anchor
希望这有帮助。