json - Use the Mozilla Backpack Connecti API with PHP -
i issue badge mozilla backpack connect api (check !). so, have followed this document still cannot issue badge !
i have exact same problem when try new access token using refresh token. i've posted "get new access token" code here because it's bit easier understand issuing one.
i in php curl, not in javascript.
here 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 trying new access token, mentioned in the same document, unfortunately, response :
bad request: bad request
@ 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/middleware/bodyparser.js:54:23
@ incomingmessage. (/var/www/openbadges/node_modules/express/node_modules/connect/lib/middleware/json.js:74:60)
@ incomingmessage.emit (events.js:92:17)
@ _stream_readable.js:938:16
@ process._tickcallback (node.js:419:13)
if deeper, have verbosed request , gives me :
> post /api/token http/1.1
host: backpack.openbadges.org
accept: /
content-type: application/json
content-length: 81* upload sent off: 81 out of 81 bytes
* additional stuff not fine transfer.c:1037: 0 0
* http 1.1 or later 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 host backpack.openbadges.org left intact
* closing connection #0
so, basically, responses me error 400 "bad request" no more information...
for information, if try javascript, works. if :
$.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 success, when use php curl doesn't work ! why ?
and badge valid (it passes the validation without problem).
i've searched hours ! solution simple not obvious...
on line : curl_setopt($ch, curlopt_postfields, http_build_query($data));
have use json_encode()
instead of http_build_query()
!
so doing : curl_setopt($ch, curlopt_postfields, json_encode($data));
, works ! yay !
Comments
Post a Comment