Curl 无法使用OAuth(Google API)进行授权

Curl 无法使用OAuth(Google API)进行授权,curl,oauth,oauth-2.0,google-oauth,google-analytics-api,Curl,Oauth,Oauth 2.0,Google Oauth,Google Analytics Api,我试图使用Curl来授权使用Oauth,但是我得到了一个缺少的必需参数:来自Google的grant_类型响应。谁能告诉我我的代码有什么问题吗 $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: www.googleapis.com', 'Content-Type: application/x-www-form-urlencoded; charset=utf-8')); curl_setopt($cur

我试图使用Curl来授权使用Oauth,但是我得到了一个缺少的必需参数:来自Google的grant_类型响应。谁能告诉我我的代码有什么问题吗

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host: www.googleapis.com', 'Content-Type: application/x-www-form-urlencoded; charset=utf-8'));
curl_setopt($curl, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v3/token');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('client_id' => 'xxxxxxxxxxx.apps.googleusercontent.com', 'client_secret' => 'xxxxxxxxxx', 'refresh_token' => $auth->refresh_token, 'grant_type' => 'refresh_token'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

您正在将一个数组传递给CURLOPT_POSTFIELDS,这导致内容类型被设置为multipart/form data,因为它是在CURLOPT_HTTPHEADER选项之后应用的。您需要首先对数组应用http_build_查询,这将导致内容类型为application/x-www-form-urlencoded。因此,请使用:


使用GoogleOAuth操场遍历Oauth流并记录http请求。那就让你的去比赛吧。
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array('client_id' => 'xxxxxxxxxxx.apps.googleusercontent.com', 'client_secret' => 'xxxxxxxxxx', 'refresh_token' => $auth->refresh_token, 'grant_type' => 'refresh_token')));