我正在尝试使用REST API注册匿名用户。我找到了几个关于链接Twitter / Facebook用户的讨论,这些用户共享相同的问题,但没有人给出这个解决方案。
根据Parse.com REST API Doc,您只需要为authData
提供某种ID,即UDID,但它会抱怨没有密码。这是错误:
" {"代码":201,"错误":"缺少用户密码"}
这是我的代码:
func registerAnonymisUserWithComplitionHandler(deviceUDID:String, onComplition: RESTResponse){
let request = NSMutableURLRequest()
request.HTTPMethod = "POST"
request.addValue("APP ID", forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue("API Key", forHTTPHeaderField: "X-Parse-REST-API-Key")
request.addValue("1", forHTTPHeaderField: "X-Parse-Revocable-Session")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let urlString = "https://api.parse.com"
let baseURL = NSURL(string: urlString)
let url = NSURL(string: "/1/users", relativeToURL: baseURL!)
request.URL = url
let param = ["authData": ["anonymous":["id":deviceUDID]]]
let session = NSURLSession.sharedSession()
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(param, options: .PrettyPrinted)
} catch {
print("error")
}
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
if (error == nil){
do {
let tmpUserDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
print(tmpUserDictionary)
let statusResponse = response as! NSHTTPURLResponse
onComplition(jsonDictionary: tmpUserDictionary, response: statusResponse, error: nil)
} catch {
}
} else {
onComplition(jsonDictionary: nil, response: nil, error: error)
}
}
task.resume()
}
感谢Muhammad Junaid Butt,我已在param
修复了一个小问题,但它确实有效。