在英特尔 XDK 中为 Parse.com REST API 或 DreamFactory 创建 Web 服务 [英] Create a Web Service in Intel XDK for Parse.com REST API or DreamFactory

查看:23
本文介绍了在英特尔 XDK 中为 Parse.com REST API 或 DreamFactory 创建 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人成功地在 XDK 中为 DreamFactory 或 Parse.com REST API 创建新的 Web 服务?我可以通过 curl 从命令行获得响应,所以看起来它应该是可行的.到目前为止,我还无法让它们中的任何一个工作.

Has anyone had success creating a new web service in XDK for either DreamFactory or Parse.com REST APIs? I'm able to get a response via curl from a command line for both, so it seems like it should be doable. So far I haven't been able to make either of them work.

作为参考,这些是 curl api 调用:

For reference, these are the curl api calls:

DreamFactory (需要先调用 session opener):

$ curl -X POST http://ec2-[my server].compute.amazonaws.com:80/rest/user/session -H "X-DreamFactory-Application-Name: testapp" -d '{"email": "test@example.com", "password" : "[my password]"}'


(这将返回一个包含会话 ID 的大型 JSON 字符串,下面使用)

$ curl -X GET http://ec2-[my server].compute.amazonaws.com:80/rest/testapp/roles -H "X-    DreamFactory-Application-Name: testapp" -H "X-DreamFactory-Session-Token: [my session]"
{"record":[{"id":1,"rolename":"Agent","description":"agent"},{"id":2,"rolename":"Client","description":"client"},{"id":3,"rolename":"Admin","description":"administrator"}]}


Parse.com:

$ curl -X GET   -H "X-Parse-Application-Id: [my appid]"   -H "X-Parse-REST-API-Key: [my api key]"   https://api.parse.com/1/classes/TestObject
{"results":[{"foo":"bar","createdAt":"2014-07-19T22:07:52.874Z","updatedAt":"2014-07-19T22:07:52.874Z","objectId":"jSpF1RrOy4"}]}


我是 JSON 的新手,所以我怀疑我的一个或多个 apiconfig.json、testapp.json 或 testapp.js 文件有问题.我已经对它们进行了足够的试验,现在它们有点乱,但如果有帮助,我可以发布它们.我希望为这些 API 或类似 API 成功创建 XDK Web 服务的人可以提供一些指导.


I'm new to JSON, so I suspect something is wrong in one or more of my apiconfig.json, testapp.json, or testapp.js files. I've experimented with them enough that they're kind of a mess now, but I can post them if it'll help. I'm hoping someone who has successfully created an XDK web service for either of these APIs or one like them can provide some guidance.

谢谢!

推荐答案

复制自 XDK 支持工程师对其 HTML5 开发论坛 (http://go.shr.lc/1nr6fsT):

Copied from an XDK support engineer's reply to a separate question on their HTML5 dev forum (http://go.shr.lc/1nr6fsT):

对于在 URL 字符串中需要两个键的 API,将这些字段添加到 apiconfig.json 文件中:

For APIs that need two keys in the URL string, add these fields to the apiconfig.json file:

"auth": "key",
"keyParam": "apiKey",
"signature": "apiSecret"

可以通过 .js 文件中的凭据.apiKey 和凭据.apiSecret 访问键值.

The key values can be accessed as credentials.apiKey and credentials.apiSecret in the .js file.

对于通过标头需要 2 个密钥的 API.将所需的头文件放在一个变量 key_info 中,并在 .js 文件中使用:

For APIs that need 2 keys via the headers. Put the required headers in a variable, key_info, and in the .js file, use this:

return $.ajax({url: url, 
               data: key_info
              });

在您发布的代码中,apiconfig.json 文件中列出的服务名称是parsedbtest",而文件名称是parsetestdb.js"和parsetestdb.json".通过将 apiconfig 条目更改为:

In the code you have posted, the service name listed in apiconfig.json file is 'parsedbtest' while the file names are 'parsetestdb.js' and 'parsetestdb.json'. Fix this by changing the apiconfig entry to:

{
    "parsetestdb": {
        "name": "test db parse.com",
        "dashboardURL": "https://www.parse.com/docs/rest",
        "auth": "key",
        "keyParam": "apiKey",
        "signature": "apiSecret"
    }
}

那么你的 parsetestdb.js 将是:

Then your parsetestdb.js will be:

(function (credentials) {
  var exports = {};
  exports.TestObject = function (params) {
    var url = 'https://XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:javascript-key='+ credentials.apiKey +'@api.parse.com/1/classes/TestObject'; //Or credentials.apiSecret.
    return $.ajax({url: url});
  };
  return exports;
})

您还可以使用 params.objectId 和 params.foo 输入其他参数.

You can also use params.objectId and params.foo to enter other parameters.

此外,上面使用的方法名称是TestObject".这应该与 .json 文件中的方法名称匹配(因此,没有空格).所以 parsetestdb.json 将是:

Also, the method name used above is 'TestObject'. This should match the method name in the .json file (thus, no whitespaces). So the parsetestdb.json will be:

{
   "endpoints":[
      {
         "name":"Methods",
         "methods":[
            {
               "MethodName":"TestObject",
               "HTTPMethod":"GET",
               "URI":"TestObject",
               "RequiresOAuth":"N",
               "parameters":[
                   {
                     "Name":"objectId",
                     "Required":"N",
                     "Location":"query",
                     "Type":"string"
                  },
                  {
                     "Name":"foo",
                     "Required":"N",
                     "Location":"query",
                     "Type":"string"
                  }
               ]
            }
         ]
      }
   ]
}

这篇关于在英特尔 XDK 中为 Parse.com REST API 或 DreamFactory 创建 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆