[英]Use the Mozilla Backpack Connect API with PHP
I would like to issue a badge with the Mozilla Backpack Connect API ( check this ! ). 我想使用Mozilla Backpack Connect API发行徽章( 请选中此! )。 To do so, I have followed this document but I still cannot issue a badge !
为此,我已经遵循了本文档,但是仍然无法颁发徽章!
I have the exact same problem when I just try to get a new access token using the refresh token. 当我尝试使用刷新令牌获取新的访问令牌时,我遇到了完全相同的问题。 So I've posted the "get new access token" code here because it's a bit easier to understand than the issuing one.
因此,我在此处发布了“获取新的访问令牌”代码,因为它比发出代码更容易理解。
I would like to do this in PHP with cURL , not in Javascript . 我想用cURL而不是Javascript在PHP中执行此操作。
Here is my code : 这是我的代码:
$data = array(
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken
);
$url = $apiRoot .'/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'w+'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
Here I just trying to get a new access token, as mentioned in the same document , but unfortunately, I always get this response : 如同一文档中所述,在这里我只是尝试获取一个新的访问令牌,但是不幸的是,我总是得到以下响应:
Bad Request: Bad Request
错误请求:错误请求
at next (/var/www/openbadges/node_modules/express/node_modules/connect/lib/proto.js:125:13)在下一个(/var/www/openbadges/node_modules/express/node_modules/connect/lib/proto.js:125:13)
at /var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:54:23在/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:54:23
at IncomingMessage.在IncomingMessage。 (/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60)
(/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60)
at IncomingMessage.emit (events.js:92:17)在IncomingMessage.emit(events.js:92:17)
at _stream_readable.js:938:16在_stream_visible.js:938:16
at process._tickCallback (node.js:419:13)在process._tickCallback(node.js:419:13)
If I look deeper, I have verbosed the request and it gives me this : 如果我看起来更深入,则我已详细说明了请求,它给了我以下信息:
> POST /api/token HTTP/1.1
> POST / api /令牌HTTP / 1.1
Host: backpack.openbadges.org主持人:backpack.openbadges.org
Accept: /接受: /
Content-Type: application/json内容类型:application / json
Content-Length: 81内容长度:81
* upload completely sent off: 81 out of 81 bytes
*上传完全发送:81个字节(共81个字节)
* additional stuff not fine transfer.c:1037: 0 0*多余的东西无法正常传输。c:1037:0 0
* HTTP 1.1 or later with persistent connection, pipelining supported*具有持续连接的HTTP 1.1或更高版本,支持管道传输
< HTTP/1.1 400 Bad Request<HTTP / 1.1 400错误请求
< Cache-control: no-cache="set-cookie"<缓存控制:no-cache =“ set-cookie”
< Content-Type: text/plain<内容类型:文本/纯文本
< Date: Fri, 29 May 2015 12:36:03 GMT<日期:2015年5月29日,星期五,格林尼治标准时间
< Set-Cookie: AWSELB=674101290634B07D75A3C1417FA6788D6E65270EC8D2D0E6014FB81FA4E878CAEA117D6E6334DB190F94A3D84909E9928F08D6B81651BDC3386AFC0A84F3A39F4B51E09B31;PATH=/;MAX-AGE=3600<Set-Cookie:AWSELB = 674101290634B07D75A3C1417FA6788D6E65270EC8D2D0E6014FB81FA4E878CAEA117D6E6334DB190F94A3D84909E9928F08D6B81651BDC3386AFC0A84F3A39F4B51E09B31PATH;
< x-frame-options: DENY<x帧选项:DENY
< X-Powered-By: Express<X-Powered-By:快速
< Content-Length: 478<内容长度:478
< Connection: keep-alive<连接:保持活动状态
<<
* Connection #0 to host backpack.openbadges.org left intact*与主机Backpack.openbadges.org的连接#0完好无损
* Closing connection #0*关闭连接#0
So, basically, it responses me an error 400 "Bad Request" with no more information... 因此,基本上,它会在没有更多信息的情况下响应我一个错误400“错误请求” ...
For information, if I try to do it with Javascript , it works. 有关信息,如果我尝试使用Javascript可以运行。 If I do this :
如果我这样做:
$.ajax({
type: 'POST',
url: 'https://backpack.openbadges.org/api/token',
data: {
grant_type: 'refresh_token',
refresh_token: theRefreshToken
},
headers: {
'Content-Type': 'application/json'
},
success: function(data, textStatus, req) {
console.log(textStatus);
console.log(data);
},
error: function(xhr, textStatus, err) {
console.log(textStatus);
console.log(err);
console.log(xhr);
console.log($(this));
}
});
This returns me a success, but when I use the PHP cURL it doesn't work ! 这使我成功,但是当我使用PHP cURL时,它不起作用! So why ?
所以为什么 ?
And my badge is valid (it passes the validation without problem). 而且我的徽章是有效的(它顺利通过了验证 )。
I've searched for hours ! 我已经搜索了几个小时! The solution is really simple but not so obvious...
解决方案确实很简单,但不是那么明显。
On this line : curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
在这行上:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
you have to use json_encode()
instead of http_build_query()
! 您必须使用
json_encode()
而不是http_build_query()
!
So doing like this : curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
所以这样做:
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
and it works ! 而且有效! Yay !
好极了 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.