繁体   English   中英

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

[英]cURL working in postman, not in Terminal (for basecamp)

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

cURL请求在POSTMAN上工作正常(选项卡: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配置。

对你的帮助表示感谢。

$data构造为如下数组:

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

并将其传递给:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

这将确保您的值已正确进行URL编码,并且Content-Type标头设置为multipart/form-data ,这显然与POSTman兼容。

我怀疑问题确实与redirect_uri的URL编码有关,所以如果Basecamp恰好需要Content-Type of application/x-www-form-urlencoded而不是multipart/form-data ,则可以使用相同的$data数组如上构造,然后使用:

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

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

进行以下较小更改即可达到目的:

$refresh_token = rtrim($refresh_token);

事实证明,我必须先删除多余的不可见字符(在这种情况下为字符串末尾字符),然后再将其发送到basecamp。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM