使用 Mozilla Backpack Connect API 和 PHP
Use the Mozilla Backpack Connect API with PHP
我想用 Mozilla Backpack Connect API 颁发徽章(check this !). To do so, I have followed this document 但我仍然不能颁发徽章!
当我尝试使用刷新令牌获取新的访问令牌时,我遇到了完全相同的问题。所以我在这里发布了 "get new access token" 代码,因为它比发布代码更容易理解。
我想在 PHP 中使用 cURL 执行此操作,而不是在 Javascript.
这是我的代码:
$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);
这里我只是想获取一个新的访问令牌,如 the same document 中所述,但不幸的是,我总是得到这样的响应:
Bad Request: Bad Request
at next (/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
at IncomingMessage. (/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60)
at IncomingMessage.emit (events.js:92:17)
at _stream_readable.js:938:16
at process._tickCallback (node.js:419:13)
如果我看得更深,我已经详细说明了请求,它给了我这个:
> POST /api/token HTTP/1.1
Host: backpack.openbadges.org
Accept: /
Content-Type: application/json
Content-Length: 81
* upload completely sent off: 81 out of 81 bytes
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 400 Bad Request
< Cache-control: no-cache="set-cookie"
< Content-Type: text/plain
< Date: Fri, 29 May 2015 12:36:03 GMT
< Set-Cookie: AWSELB=674101290634B07D75A3C1417FA6788D6E65270EC8D2D0E6014FB81FA4E878CAEA117D6E6334DB190F94A3D84909E9928F08D6B81651BDC3386AFC0A84F3A39F4B51E09B31;PATH=/;MAX-AGE=3600
< x-frame-options: DENY
< X-Powered-By: Express
< Content-Length: 478
< Connection: keep-alive
<
* Connection #0 to host backpack.openbadges.org left intact
* Closing connection #0
所以,基本上,它会回复我一个错误 400 "Bad Request",没有更多信息...
有关信息,如果我尝试使用 Javascript,它会起作用。如果我这样做:
$.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));
}
});
这 returns 我成功了,但是当我使用 PHP cURL 时它不起作用!那为什么?
而且我的徽章是有效的(它通过 the validation 没有问题)。
我搜索了几个小时!解决方案非常简单但不是很明显...
这一行:curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
你必须使用 json_encode()
而不是 http_build_query()
!
所以这样做:curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
它有效!耶!
我想用 Mozilla Backpack Connect API 颁发徽章(check this !). To do so, I have followed this document 但我仍然不能颁发徽章!
当我尝试使用刷新令牌获取新的访问令牌时,我遇到了完全相同的问题。所以我在这里发布了 "get new access token" 代码,因为它比发布代码更容易理解。
我想在 PHP 中使用 cURL 执行此操作,而不是在 Javascript.
这是我的代码:
$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);
这里我只是想获取一个新的访问令牌,如 the same document 中所述,但不幸的是,我总是得到这样的响应:
Bad Request: Bad Request
at next (/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
at IncomingMessage. (/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60)
at IncomingMessage.emit (events.js:92:17)
at _stream_readable.js:938:16
at process._tickCallback (node.js:419:13)
如果我看得更深,我已经详细说明了请求,它给了我这个:
> POST /api/token HTTP/1.1
Host: backpack.openbadges.org
Accept: /
Content-Type: application/json
Content-Length: 81* upload completely sent off: 81 out of 81 bytes
* additional stuff not fine transfer.c:1037: 0 0
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 400 Bad Request
< Cache-control: no-cache="set-cookie"
< Content-Type: text/plain
< Date: Fri, 29 May 2015 12:36:03 GMT
< Set-Cookie: AWSELB=674101290634B07D75A3C1417FA6788D6E65270EC8D2D0E6014FB81FA4E878CAEA117D6E6334DB190F94A3D84909E9928F08D6B81651BDC3386AFC0A84F3A39F4B51E09B31;PATH=/;MAX-AGE=3600
< x-frame-options: DENY
< X-Powered-By: Express
< Content-Length: 478
< Connection: keep-alive
<
* Connection #0 to host backpack.openbadges.org left intact
* Closing connection #0
所以,基本上,它会回复我一个错误 400 "Bad Request",没有更多信息...
有关信息,如果我尝试使用 Javascript,它会起作用。如果我这样做:
$.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));
}
});
这 returns 我成功了,但是当我使用 PHP cURL 时它不起作用!那为什么?
而且我的徽章是有效的(它通过 the validation 没有问题)。
我搜索了几个小时!解决方案非常简单但不是很明显...
这一行:curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
你必须使用 json_encode()
而不是 http_build_query()
!
所以这样做:curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
它有效!耶!