I’m using fabric to post a tweet with a photo
At the start up of my application I call Twitter.sharedInstance().logInWithCompletion, and this call succeeds.
Then I want to post a tweet with a photo, the photo upload goes well, and I receive the media_id_string.
I use the response of this call to construct the parameters of the status update call.
But this call always fails with :
Error Domain=TWTRNetworkingErrorDomain Code=-1011 “Request failed: unauthorized (401)” UserInfo=0x174263440 {NSErrorFailingURLKey=https://api.twitter.com/1.1/statuses/update.json, NSLocalizedDescription=Request failed: unauthorized (401), NSLocalizedFailureReason=Twitter API error : (code (null))}
Can someone please point out to me why I am getting this 401, while I am clearly signed in (otherwise the upload call would fail too right?)
My code:
class func postToFabricTwitter(krumb:Krumb, imageUrl:NSURL, handler:(success:Bool, error:NSError?) -> ()) {
let strUploadUrl = "https://upload.twitter.com/1.1/media/upload.json"
let strStatusUrl = "https://api.twitter.com/1.1/statuses/update.json"
var parameters:[String:String] = [:]
let imageData = NSData(contentsOfURL: imageUrl)!
parameters["media"] = imageData.base64EncodedStringWithOptions(nil)
var uploadRequestError: NSError?
var twUploadRequest = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST", URL: strUploadUrl, parameters: parameters, error: &uploadRequestError)
if twUploadRequest != nil {
Twitter.sharedInstance().APIClient.sendTwitterRequest(twUploadRequest) {
(uploadResponse, uploadResultData, uploadConnectionError) -> Void in
if (uploadConnectionError == nil) {
if let parameters = self.getParametersForStatusUpdateFromResponse(uploadResultData) {
println("result = \(parameters)")
var statusRequestError:NSError?
var twStatusRequest = Twitter.sharedInstance().APIClient.URLRequestWithMethod("POST", URL: strStatusUrl, parameters: parameters, error: &statusRequestError)
if (twStatusRequest != nil)
{
Twitter.sharedInstance().APIClient.sendTwitterRequest(twStatusRequest) { (statusResponse, statusData, statusConnectionError) -> Void in
if (statusConnectionError != nil) {
println("Error posting status \(statusConnectionError)")
}
else {
var jsonError : NSError?
let json : AnyObject? =
NSJSONSerialization.JSONObjectWithData(statusData,
options: nil,
error: &jsonError)
println("\(json)")
}
}
} else {
println("Error creating status request \(statusRequestError)")
}
} else {
println("parameters where nil")
}
} else {
println("Error uploading image \(uploadConnectionError)")
}
}
} else {
println("Error creating upload request \(uploadRequestError)")
}
}
class func getParametersForStatusUpdateFromResponse(uploadResponse:NSData) -> [String:String]?{
var parameters:[String:String] = [:]
var parsingError: NSError?
let json: [String:AnyObject] = NSJSONSerialization.JSONObjectWithData(uploadResponse, options: NSJSONReadingOptions.MutableContainers, error: &parsingError) as [String:AnyObject]
// check for media id in result
if (json["media_id_string"] != nil) {
println("result = \(json)")
// post a status with link to media
parameters["status"] = "Hey look at this"
parameters["media_ids"] = json["media_id_string"] as? String
return parameters
}
else {
println("Media_id not found in result = \(json)")
return nil
}
}