I am trying to sign up an anonymous user using REST API. I found several discussion regarding linking Twitter/Facebook users that share the same problem but none gave this solution.
According to Parse.com REST API Doc, you only need to provide some sort of id to the authData, i.e UDID, but it complains that there is no password. This is the error:
"{"code":201,"error":"missing user password"}
This is my code:
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()
}
Thanks to Muhammad Junaid Butt, I have fixed a small issue in the param and it works.
