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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader