dongmale0656 2015-05-29 14:19 采纳率: 100%
浏览 43
已采纳

使用Mozilla Backpack Connect API和PHP

I would like to issue a badge with the Mozilla Backpack Connect API (check this !). 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.

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)
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)

If I look deeper, I have verbosed the request and it gives me this :

> 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

So, basically, it responses me an error 400 "Bad Request" with no more information...

For information, if I try to do it with Javascript, it works. 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 ! So why ?

And my badge is valid (it passes the validation without problem).

  • 写回答

1条回答 默认 最新

  • du7979 2015-05-29 14:19
    关注

    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)); you have to use json_encode() instead of http_build_query() !

    So doing like this : curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); and it works ! Yay !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作