After logging in and authenticating with Twitter I’m able to successfully post a tweet, just the message, to the update url.
The constants…
let kTwitterPOSTmethod = "POST"
let kTwitterUpdateURL = "https://api.twitter.com/1.1/statuses/update.json"
let kTwitterUploadURL = "https://upload.twitter.com/1.1/media/upload.json"
Twitter client stuff…
let store = Twitter.sharedInstance().sessionStore
if let userid = store.session()?.userID {
let client = TWTRAPIClient(userID: userid) //from logInWithCompletion() in the previous VC
....
}
Function that works, when base64Image string is “”…
if snapBase64String == "" {
//just text (works!)
var updateParams : [String : AnyObject] = ["status" : withMessage]
if let location = currentLocation {
updateParams = ["status" : withMessage, "lat" : String(Float(location.latitude)), "long" : String(Float(location.longitude))]
}
//TODO: Handle error properly (do / catch?)
let updateRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUpdateURL, parameters: updateParams as [NSObject : AnyObject], error: nil)
//Lastly, we sent the status update along with the image that we parsed from the earlier request
client.sendTwitterRequest(updateRequest, completion: { (response, data, connectionError) -> Void in
if connectionError == nil {
print("sendTwitterRequest(): (response)")
complete(success: true)
}
else {
print("sendTwitterRequest(): (connectionError)")
complete(success: false)
}
})
}
Function that doesn’t work, when image is included. I post to the upload url and try to grab the media_id_string with a JSON serializer function but it tells me I’m unauthorized.
else {
let postImageWithStatusRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUploadURL, parameters: ["media": snapBase64String], error: nil)
client.sendTwitterRequest(postImageWithStatusRequest, completion: { (response, data, error) in
if error != nil {
print(error?.localizedDescription)
}
if let mediaDict = self.dataToJSON(data!) {
let message = ["status": withMessage, "media_ids": mediaDict["media_id_string"]]
let request = client.URLRequestWithMethod(kTwitterPOSTmethod,
URL: kTwitterUpdateURL, parameters: message as! [String : AnyObject], error:nil)
client.sendTwitterRequest(request, completion: { (response, data, connectionError) -> Void in
if connectionError == nil {
print("sendTwitterRequest(): (response)")
complete(success: true)
}
else {
print("sendTwitterRequest(): (connectionError)")
complete(success: false)
}
})
}
})
}
JSON converting function
class func dataToJSON(data: NSData) -> AnyObject? {
do {
return try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers)
} catch let myJSONError {
print(myJSONError)
}
return nil
}
The error message I get… do I need different authentication to be able to post to the upload part of the server? I’m not sure why it lets me post a status but when I want to post media it doesn’t work.
▿ Optional
– Some : Error Domain=TwitterAPIErrorDomain Code=32 “Request failed: unauthorized (401)”
UserInfo={NSErrorFailingURLKey=https://upload.twitter.com/1.1/media/upload.json,
NSLocalizedDescription=Request failed: unauthorized (401),
NSLocalizedFailureReason=Twitter API error : Could not authenticate
you. (code 32)}
Thanks for the help!
2
Answers
I figured it out, it had nothing to do with authentication, the error was very misleading.
What I did was change the way I encoded the base 64 string. I just set the options to nothing instead of the 64CharacterLineLength one and it worked!
Below is my working code.