首页 \ 问答 \ 使用Mozilla Backpack Connect API和PHP(Use the Mozilla Backpack Connect API with PHP)

使用Mozilla Backpack Connect API和PHP(Use the Mozilla Backpack Connect API with PHP)

我想发布一个带有Mozilla Backpack Connect API的徽章( 请查看! )。 为此,我已按照此文件,但我仍然无法签发徽章!

当我尝试使用刷新令牌获取新的访问令牌时,我遇到了完全相同的问题。 所以我在这里发布了“获取新访问令牌”代码,因为它比发布代码更容易理解。

我想用cURLPHP中执行此操作,而不是在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);

在这里,我只是尝试获取一个新的访问令牌,如同一文档中所述 ,但不幸的是,我总是得到这样的回应:

错误请求:错误请求
在下一个(/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
在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
at process._tickCallback(node.js:419:13)

如果我看得更深,我已经对请求进行了详细说明,它给了我这个:

> POST / api / token HTTP / 1.1
主持人:backpack.openbadges.org
接受: /
Content-Type:application / json
内容长度:81

*上传完全被发送:81个字节中的81个
*额外的东西不精细transfer.c:1037:0 0
*具有持久连接的HTTP 1.1或更高版本,支持流水线操作
<HTTP / 1.1 400错误请求
<Cache-control:no-cache =“set-cookie”
<Content-Type:text / plain
<日期:2015年5月29日星期五12:36:03 GMT
<Set-Cookie:AWSELB = 674101290634B07D75A3C1417FA6788D6E65270EC8D2D0E6014FB81FA4E878CAEA117D6E6334DB190F94A3D84909E9928F08D6B81651BDC3386AFC0A84F3A39F4B51E09B31; PATH = /; MAX-AGE = 3600
<x-frame-options:DENY
<X-Powered-By:Express
<内容长度:478
<连接:保持活力
<
*连接#0主机backpack.openbadges.org完好无损
*关闭连接#0

所以,基本上,它回复了我一个错误400“错误请求”,没有更多的信息.​​.....

有关信息,如果我尝试使用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));
    }
});

这让我很成功,但是当我使用PHP cURL时它不起作用! 所以为什么 ?

我的徽章是有效的(它通过验证没有问题)。


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


原文:https://stackoverflow.com/questions/30531965
更新时间:2023-05-31 12:05

最满意答案

您可以通过servlet使代码可用,可以通过URL调用,并在tomcat服务器上运行它。

首先制作一个servlet。 创建一个扩展HttpServlet的类。

要处理HTTP GET请求,请覆盖doGet方法:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException

要处理HTTP POST请求,请覆盖doPost方法:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException

您可以使这些方法执行逻辑或检索数据,并将其公开为HTMLXMLJSON

要将servlet映射到url,需要一个web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
    <servlet-name>nameOfYourServlet</servlet-name>
    <servlet-class>com.your.package.ServletImplementationClass</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>nameOfYourServlet</servlet-name>
    <url-pattern>/api/my/servlet</url-pattern>
</servlet-mapping>
</web-app>

要将其部署到tomcat服务器,您需要将web.xml和已编译的类存放在WEB-INF文件夹中。 将WEB-INF文件夹压缩到存档中,将存档的扩展名更改为war,然后将其放入tomcat webapps文件夹中。

我认为这是实现服务器逻辑运行的最快方式,但您的实现方式取决于您想要实现的目标,以及您要使用的服务器等。

你可能值得花些时间研究一下servlet。 看到这个链接。


You can make code available through a servlet, callable by a URL, and run it on a tomcat server.

You start out by making a servlet. Create a class that extends HttpServlet.

To handle HTTP GET requests, override the doGet method:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException

To handle HTTP POST requests, override the doPost method:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException

You can make these methods execute logic, or retrieve data, and expose it as something like HTML, XML or JSON.

To map the servlet to the url, you need a web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
    <servlet-name>nameOfYourServlet</servlet-name>
    <servlet-class>com.your.package.ServletImplementationClass</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>nameOfYourServlet</servlet-name>
    <url-pattern>/api/my/servlet</url-pattern>
</servlet-mapping>
</web-app>

To deploy this to a tomcat server, you need your web.xml and compiled classes to live inside a WEB-INF folder. Zip the WEB-INF folder into an archive, change the extension of the archive to war, and drop it in your tomcat webapps folder.

I think this is about the quickest way to get server logic running, but your way of implementation depends on what you want to achieve, and what server you want to use etc.

It's probably worth your time looking into servlets a bit more. See this link.

相关问答

更多
  • 通常情况下,避免重复生成的代码非常困难,但常见的方法是使用代码生成器来构建服务器或客户端代码,以便只编写一半代码。 最流行的方法是编写服务器端公共代码,然后让代码生成器为您构建JavaScript代码。 例如,我们公司使用的语言是Coldfusion, Form-o-matic为我们解决了这个问题。 人们也通过编写可以在服务器端执行的JavaScript来从相反的方向解决问题。 我会寻找一个框架,为你做到这一点。 Typically, it's really hard to avoid duplicatin ...
  • 更新(2017年3月10日) : 尽管下面概述的体系结构仍然有效,并且可用于将Firebase与任何现有基础结构结合使用,但Firebase刚刚发布了Firebase的Cloud Functions ,它允许您在Google服务器上运行JavaScript功能以响应Firebase事件(如数据库更改,用户登录等等)。 一种可能的解决方案(未经测试,很抱歉;但它应该是正确的想法): { "rules": { "users": { "$user": { /* When t ...
  • Stripe的典型付款流程可分为两个步骤: 在客户端,您收集客户的付款信息并将其转换为令牌。 在Web应用程序中,这是通过Checkout或Stripe.js完成的 。 在移动应用程序中,这是通过Stripe的iOS或Android SDK完成的。 创建令牌后,将其发送到后端服务器。 在服务器端,您使用令牌创建费用 ,或创建将保存付款信息的客户 ,以便将来可以创建更多费用,而无需再次收取付款信息。 本教程解释了流程的服务器端部分。 这个两步流程的主要优点是您的服务器永远不会处理PCI敏感的卡信息。 它仅处理 ...
  • 简短回答:您的管道定义看起来正确。 您需要确保运行最新版本的Task Runner。 我会尝试重现您的问题并通知您。 PS让我们在这里或AWS Data Pipeline论坛中的单个线程中保持对话,以避免混淆。 在官方AWS Data Pipeline论坛页面上回答 This issue is resolved when I downloaded new TaskRunner-1.0.jar. I was running older version.
  • 您可以通过servlet使代码可用,可以通过URL调用,并在tomcat服务器上运行它。 首先制作一个servlet。 创建一个扩展HttpServlet的类。 要处理HTTP GET请求,请覆盖doGet方法: @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException 要处理HTTP POST请求,请覆盖doPost方法: public void doP ...
  • 他没有更好地理解游戏中的问题空间,他说: 老实说,你最好只写一个手机可以通过HTTP联系的网络服务。 所有的API都已经存在于Android和您想要使用的任何服务器端技术(Java,Python,Ruby,自我敲门)。 编写一个定制的套接字连接器是相当古老的... 1990年,他们希望他们的专有套接字协议回来... Without better understanding of the problem space in play, he says: Honestly, you're better off j ...
  • 听起来你在JS代码中有配置值(即字幕)。 如果你想保持这种方式,我的建议是将配置本身拆分成一个离散的.JS文件,它只包含变量定义但不包含实际的JS代码。 然后你应该能够使用json_decode()在PHP中简单地解析它。 任务完成。 It sounds like you have config values (ie the captions) in the JS code. If you want to keep it this way, my suggestion would be to split t ...
  • 由于这通常是问题,因此您对权限进行集中是完全正确的。 如果EXE进程启动并且什么都不做,那么它可能仅用于交互式使用,并且正在提示某些内容。 看这个页面它说: CPCTool.exe运行的第一个时间提示用户从笛卡尔读取/接受用户协议 - 必须接受此协议或转换器不起作用。 我认为这就是它正在做的事情。 尝试以应用程序池用户身份登录,启动程序并接受协议。 You are completely correct to concentrrate on the permissions as that is usually ...
  • 服务器端,意味着某些东西在服务器上工作,或者它在安装IIS的计算机上工作。 客户端意味着某些东西在浏览器中工作。 如果要谈论体系结构XAML,它的.cs代码和与silverlight部分相关的任何内容都将被编译到.XAP文件中,并通过Silverlight插件在浏览器中执行,因此它是客户端。 因此,服务器端分页意味着您需要在服务器上创建一些C#代码,这将只返回您请求的页面。 例如,您有160条记录,并且您想在单个页面上显示10条记录。 每次您向客户端(或在浏览器中工作的东西)提供160条记录时,如果没有服务 ...
  • 如果我理解你的问题你想要的是像phantom.js 。 PhantomJS是一个带有JavaScript API的无头WebKit。 你可以将jquery注入其中并使用所有jquery选择器来操作dom。 你可以让它像一个独立的服务器一样工作。 if i understand your question correctly you want is something like phantom.js. PhantomJS is a headless WebKit with JavaScript API. yo ...

相关文章

更多

最新问答

更多
  • 在开发React应用程序时编译/转换代码(Compile/transpile code while developing React app)
  • 将MultiPoint序列化为GeoJSON文件(Serialize MultiPoint to GeoJSON file)
  • 将字符串截断为特定数量的字符,忽略HTML(Truncate string to certain amount of characters, ignoring HTML)
  • Sqlserver监视时间的变化(Sqlserver watch for time change)
  • Srcset属性 - 最大宽度问题(Srcset attribute - max-width issue)
  • 如何验证数据库中是否存在记录?(How to verify existence of a record in database?)
  • SQL JOIN来自不同表的行具有相同的值(SQL JOIN row from different table with the same values)
  • NSTextField - 使用KVO进行输入验证?(NSTextField - Input validation with KVO?)
  • 在Javascript中,如何检查数组是否有重复值?(In Javascript, how do I check if an array has duplicate values? [duplicate])
  • 获取过滤器从spark数据帧中删除的行的示例(Get examples for rows that are removed by a filter from a spark dataframe)
  • 如果Shape在屏幕外,是否执行Graphics2D.draw?(Is Graphics2D.draw performed if the Shape is offscreen?)
  • 如果没有头文件,如何定义静态成员?(How to define a static member in case there is not header file?)
  • NSLocalizedStringFromTableInBundle:没有获取key的值(NSLocalizedStringFromTableInBundle : not getting values for key)
  • Google Cloud Messanger,“比预期的更多”(Google Cloud Messanger, “More acks than expected”)
  • HorizontalScrollView不起作用(HorizontalScrollView not working)
  • 关闭所有表单后退出应用程序(Quit Application when all forms is closed)
  • 玉林哪个会计培训学校有周末上课的!
  • java的。(java. Unique identifier for each visitor. How better generated?)
  • 希望大家告诉我学java编程的方法?
  • 在GAS嵌入式环境中通过Javascript刷新HTML文档(Refresh HTML Document via Javascript in a GAS Embedded Environment)
  • 当我在另一个类中调用它时,setText不起作用(setText does not work when I call it in another class)
  • 无法在静态字段中设置变量值(Can't Set variable value in Static Field)
  • IE9布局错误 - 在早期的IE版本中很好(IE9 Layout Bug - fine in earlier IE versions)
  • 按钮命令上的WPF新DataGrid行参数为NULL(WPF New DataGrid Row Parameter NULL on Button Command)
  • Wordpress上的CSS配置(CSS configuration on Wordpress )
  • 关于OpenGL设置的问题以及在窗口中绘制掩码的问题(Questions about OpenGL Settings and drawing over a mask in a window)
  • Matlab编码器fzero功能(Matlab coder fzero function)
  • Autodesk Maya,C ++和OpenGL渲染引擎(Autodesk Maya, C++ and OpenGL rendering engine)
  • 选择记录字段包含另一个字段的最大值(Select field of record contains max of another field)
  • 可选参数在Swashbuckle.AspNetCore中导致null异常(Optional parameter causes null exception in Swashbuckle.AspNetCore)