skip to Main Content

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


  1. Chosen as BEST ANSWER

    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!

    let base64String = mapSnapData.base64EncodedStringWithOptions([])
    snapBase64String = base64String
    

  2. Below is my working code.

    func shareToTwitter(){
    
    
        let store = Twitter.sharedInstance().sessionStore
        if let userid = store.session()?.userID {
            let client = TWTRAPIClient(userID: userid)
            let statusesShowEndpoint = "https://upload.twitter.com/1.1/media/upload.json"
    
            let image = UIImage(named: "How_To_Say_TY")
            let data = UIImagePNGRepresentation(image!)
            let strImage = data?.base64EncodedStringWithOptions([])
    
            let params  : [String : AnyObject] = ["media": strImage!]
            var clientError : NSError?
    
            let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
    
            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
    
                if (connectionError == nil) {
    
                    do {
                        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
                        print("test tweet :- (json["media_id_string"])")
    
                        self.updateTwitterStatusWithMediaID("(json["media_id_string"])")
    
                    } catch {
                        print("error serializing JSON: (error)")
                    }
                }
                else {
                    print("Error: (connectionError)")
                }
            }
        }
    }
    
    func updateTwitterStatusWithMediaID( mediaID : String ){
    
        let store = Twitter.sharedInstance().sessionStore
        if let userid = store.session()?.userID {
            let client = TWTRAPIClient(userID: userid)
            let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
            let params = ["status": "Test by Carl.","media_ids":mediaID]
            var clientError : NSError?
    
            let request = client.URLRequestWithMethod("POST", URL: statusesShowEndpoint, parameters: params, error: &clientError)
    
            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
    
                if (connectionError == nil) {
    
                    do {
                        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
                        print("test tweet :- (json)")
                    } catch {
                        print("error serializing JSON: (error)")
                    }
                }
                else {
                    print("Error: (connectionError)")
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search