Use the Mozilla Backpack Connect API with PHP

117 views Asked by At

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

There are 1 answers

3
Allan Mermod On BEST ANSWER

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 !