使用REST API从javascript调用parse.com作业

时间:2015-02-22 00:22:00

标签: android rest parse-platform appcelerator

正如标题所说,我正在尝试使用REST API从javascript(特别是Appcelerator Android应用程序)在parse.com上运行作业。我正在使用REST,因为这仅用于诊断,我不想处理尝试使用Appcelerator中的parse.com javascript API。问题是我无法通过身份验证。如果我没有传入身份验证标头,我会收到相应的401身份验证错误,但如果我设置了它们,我会收到“BAD REQUEST”。我已经通过cURL工作了,所以URL是正确的,并且解析会按预期响应该调用。这是我的代码:

var url = "https://api.parse.com/1/jobs/sendMail";

    var client = Ti.Network.createHTTPClient({
         // function called when the response data is available
         onload : function(e) {
             Ti.API.info("Received text: " + this.responseText);
             alert("Received text: " + this.responseText);
         },
         // function called when an error occurs, including a timeout
         onerror : function(e) {
             Ti.API.debug(e.error);
             alert(e.error);
         },
         timeout : 5000  // in milliseconds
     });

     var param = {"text":msg};

     // Prepare the connection.
     var auth = {"app":"sTnsthoeunotreallymyappIDbutabunchofcharactersESnecu","key":"8ll5thisisntreallymykeyeitherhMKqkYG"};
     client.open("POST", url);

     client.setRequestHeader("X-Parse-Application-Id",auth.app);
     client.setRequestHeader("X-Parse-REST-API-Key",auth.key);

     client.setRequestHeader("Content-Type","application/json");

     // Send the request.
     client.send(param);

以下是请求和回复:

POST https://api.parse.com/1/jobs/sendMail HTTP/1.1
X-Parse-Application-Id: myappid
User-Agent: 
X-Parse-REST-API-Key: myrestapikey
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Content-Length: 0
Host: api.parse.com
Connection: Keep-Alive

HTTP/1.1 401 Unauthorized
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Date: Sun, 22 Feb 2015 04:36:11 GMT
Server: nginx/1.6.0
Set-Cookie: _parse_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRiIlMTY4MzY0NTZlOWQ3ZGRjZDJkOWQwMjA4MWZjNWViMTY%3D--ffc760efbe32aa80a5e6d369606361413433fa72; domain=.parse.com; path=/; expires=Tue, 24-Mar-2015 04:36:11 GMT; secure; HttpOnly
Status: 401 Unauthorized
WWW-Authenticate: Basic realm="Parse"
X-Content-Type-Options: nosniff
X-Runtime: 0.018320
X-UA-Compatible: IE=Edge,chrome=1
Content-Length: 24
Connection: keep-alive

{"error":"unauthorized"}

1 个答案:

答案 0 :(得分:0)

通过REST-API触发后台作业需要使用Master-Key,而不是REST-API密钥。

https://parse.com/docs/rest#backgroundjobs