Search code examples
iostwittertwitter-oauthtwitter-fabric

TwitterKit sharing stopped working


Uploading a photo from my app to the user's Twitter account was working fine, and all of a sudden I get this error:

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)}

Here's how I start the Twitter session:

    Twitter.sharedInstance().logInWithCompletion({ (session, error) -> Void in
    })

And then here's how I upload the media to Twitter, with the error coming back from the sendtwitterRequest method:

    let store = Twitter.sharedInstance().sessionStore

    if let userid = store.session()?.userID {

      let client = TWTRAPIClient(userID: userid) //we have this from logInWithCompletion() in the previousVC

      let imageData = UIImagePNGRepresentation(self.cardView.takeSnapshot())
      let uploadParams = ["media" : imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)]
      //TODO: Handle error properly (do / catch?)
      let uploadRequest = client.URLRequestWithMethod(kTwitterPOSTmethod, URL: kTwitterUploadURL, parameters: uploadParams, error: nil)

      //First we upload the image via a request
      client.sendTwitterRequest(uploadRequest, completion: { (response, uploadResultData, error) -> Void in

..with the sendTwitterRequest method returning the error cited above.

Any ideas? I updated the TwitterKit SDK and it still gives me the "Could not authenticate you" error.


Solution

  • They've released version 2.0 of TwitterKit which contains the public upload function.