cURL在邮递员工作,而不是在终端(对于大本营)

时间:2015-02-09 08:49:15

标签: php curl postman basecamp

我正在尝试通过basecamp api刷新access_tokens。但我面临一个奇怪的错误。

cURL请求在POSTMAN上工作正常(tab:form-data)。但我在PHP中尝试了cURL的所有配置,但无法使其正常工作。

这是我正在使用的代码:

$refresh_token = func_to_get_refresh_token();

$data='redirect_uri=xxxxmyredirecturixxxx&client_id=xxxxmyclientidxxx&client_secret=xxxxmyclientsecretxxxxx&refresh_token='.$refresh_token.'&type=refresh';
curl_setopt($ch, CURLOPT_URL, 'https://launchpad.37signals.com/authorization/token');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($ch);

我从Basecamp服务器取回以下结果:

* upload completely sent off: 448 out of 448 bytes
< HTTP/1.1 400 Bad Request
* Server nginx is not blacklisted
< Server: nginx
< Date: Mon, 09 Feb 2015 08:05:51 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive 
< Status: 400 Bad Request
< X-Request-Id: aaec3a6c61eb5e603672a7a2e004ea7a
< Cache-Control: no-cache
< Set-Cookie: _launchpad_session=BAh7BiIPc2Vzc2lvbl9pZCIlOTEwZTEyOTY0N2M1ZDMxNjM4YjJlZTI2MmRjODE0MTI%3D--7ba9863975db8a7d2c97425300abab8d5405c17a; path=/; HttpOnly; secure
< X-Frame-Options: SAMEORIGIN
< X-Runtime: 0.011202
< Strict-Transport-Security: max-age=31536000
< X-UA-Compatible: IE=Edge,chrome=1
< 
* Connection #0 to host launchpad.37signals.com left intact
{"error":"authorization_expired"}

我已经在stackoverflow上找到了几乎所有cURL配置。

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

将您的$data构建为数组:

$data = array(
    'redirect_uri' => 'xxxxmyredirecturixxxx',
    'client_id' => 'xxxxmyclientidxxx',
    'client_secret' => 'xxxxmyclientsecretxxxxx',
    'refresh_token' => 'xxxrefreshtokenofmyaccountxxx',
    'type' => 'refresh'
);

并将其传递给:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

这将确保您的值正确进行网址编码,并且Content-Type标头设置为multipart/form-data,这显然适用于POSTman。

我怀疑问题确实与redirect_uri的网址编码有关,因此如果Basecamp恰好需要Content-Type application/x-www-form-urlencoded而不是multipart/form-data,那么您可以使用与上面相同的$data数组结构,但然后使用:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query ($data));

将内容类型设置为application/x-www-form-urlencoded。这仍然可以对参数进行正确的URL编码。

答案 1 :(得分:0)

以下小改动就是诀窍:

$refresh_token = rtrim($refresh_token);

事实证明,在将其发送到basecamp之前,我必须删除额外的不可见字符(在本例中为字符串结尾字符)。

答案 2 :(得分:0)

我看到完全相同的错误消息,这里的答案都没有帮助我。事实证明,Basecamp使用非常长刷新令牌,而我用来存储它们的数据库列只设置为VARCHAR(255),这就是截断刷新令牌。增加列的大小解决了这个问题。