我在使用 Parse Cloud Code 和 Xcode(swift) 时尝试从 Spotify 请求访问/刷新 token 。
var express = require("express"),
app = express(),
crypto = require('crypto'),
buffer = require('buffer'),
url = require('url');
var config = new Parse.Object("Config");
config.set("client_id", "");
config.set("client_secret", "");
config.set("callback_url", "");
config.set("endpoint", "https://accounts.spotify.com");
var AUTH_HEADER = "Basic " + new buffer.Buffer(config.get("client_id")+ ":" + config.get("client_secret")).toString("base64");
app.use(express.bodyParser());
Parse.Cloud.define("swap", function (req, res) {
if (!req.body || !req.body.hasOwnProperty("code")) {
res.status(550).send("ermission Denied");
return;
}
var form_data = {
"grant_type": "authorization_code",
"redirect_uri": config.get("callback_url"),
"code": req.body.code
};
Parse.Cloud.httpRequest({
method: "OST",
url: url.resolve(config.get("endpoint"), "/api/token"),
headers: {
"Authorization": AUTH_HEADER,
"Content-Type": "application/x-www-form-urlencoded"
},
body: form_data,
success: function(httpResponse) {
if (httpResponse.status != 200) {
res.status(550).send("ermission Denied");
return;
}
var token_data = JSON.parse(httpResponse.text);
res.status(200).set({
"Content-Type": "application/json"
}).send(token_data);
},
error: function(httpResponse) {
res.status(500).send("Internal Server Error");
return;
}
});
});
Parse.Cloud.define("refresh", function (req, res) {
if (!req.body || !req.body.hasOwnProperty("refresh_token")) {
res.status(550).send("ermission Denied");
return;
}
var refresh_token = req.body.refresh_token;
var form_data = {
"grant_type": "refresh_token",
"refresh_token": refresh_token
};
Parse.Cloud.httpRequest({
method: "OST",
url: url.resolve(config.get("endpoint"), "/api/token"),
headers: {
"Authorization": AUTH_HEADER,
"Content-Type": "application/x-www-form-urlencoded"
},
body: form_data,
success: function(httpResponse) {
if (httpResponse.status != 200) {
res.status(550).send("ermission Denied");
return;
}
res.status(httpResponse.status).set({
"Content-Type": "application/json"
}).send(httpResponse.text);
},
error: function(httpResponse) {
res.status(500).send("Internal Server Error");
return;
}
});
});
Parse.Cloud.define("api/token", function (req, res) {
res.status(200).set({
"Content-Type": "application/json"
});
switch (req.body.grant_type) {
case "authorization_code": {
res.status(200).set({
"Content-Type": "application/json"
}).send({
"refresh_token": "REFRESH TOKEN"
});
} break;
case "refresh_token": {
res.status(200).set({
"Content-Type": "application/json"
}).send({
"access_token": "ACCESS TOKEN"
});
} break;
default: {
res.status(550).set({
"Content-Type": "text/html"
}).send("<html><body><h1>Access Denied</h1></body></html>");
} break;
}
});
app.listen();
在 Xcode 中调用函数如下:
PFCloud.callFunctionInBackground("swap", withParameters: nil) { (results, error: NSError?) -> Void in
if error != nil {
// Your error handling here
print(error)
} else {
// Deal with your results (votes in your case) here.
print(results)
}
}
我收到以下错误:
[Error]: TypeError: Object # has no method 'status'
在 main.js:29:13(代码:141,版本:1.9.1)
可选(错误域=解析代码=141“类型错误:对象#没有方法'状态'
在 main.js:29:13"UserInfo={code=141,temporary=0,error=TypeError: Object # has no method 'status'
在 main.js:29:13,NSLocalizedDescription=TypeError: Object # has no method 'status'
在 main.js:29:13})
谢谢!
Best Answer-推荐答案 strong>
在处理完这段确切的代码并试图让它工作后,我终于意识到这是 node.js 代码,而不是 Parse 云代码。如果您仍想使用它,我建议您阅读 Parse 的有关将 node.js 与 Parse 结合使用的文档。否则,你最好做点别的事情,比如在 Heroku 上启动 Spotify 提供的 ruby 脚本。
如果您喜欢冒险,可以尝试将代码移植到 Parse Cloud 代码格式,但这似乎相当困难
关于ios - 对象没有方法 'Status' iOS SDK 和解析云代码,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/34193712/
|