skip to Main Content

I am attempting to use OAuth 1 with the Twitter API using OAuthSwift. I have completed all of the setup steps while building successfully throughout, but on the last step I am getting an error. When I implement the following code, I get an error saying “The operations cannot be completed. (OAuthSwiftError error -10)”. I am thinking it might have something to do with the callback URL but it is very unclear and there isn’t much documentation on this error. Or maybe there is something wrong with my key or secret? I have copied them directly from the Twitter dev site.

let oauthswift = OAuth1Swift(
            consumerKey:    CONSUMER_KEY,
            consumerSecret: CONSUMER_SECRET,
            requestTokenUrl: "https://api.twitter.com/oauth/request_token",
            authorizeUrl:    "https://api.twitter.com/oauth/authorize",
            accessTokenUrl:  "https://api.twitter.com/oauth/access_token"
        )
let _ = oauthswift.authorize(
            withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
            success: { credential, response, parameters in
                print(credential.oauthToken)
                print(credential.oauthTokenSecret)
                print(parameters["user_id"])
                // Do your request
        },
            failure: { error in
                print(error.localizedDescription)
        }
        )

2

Answers


  1. Chosen as BEST ANSWER

    I needed to create class attributes. handle and oauthswift are declared as attributes of the class and now the code works. Revised code below:

    var oauthswift: OAuth1Swift!
        var handle: OAuthSwiftRequestHandle!
        var newOAuthToken: String!
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            print("BEFORE OAUTHSWIFT")
            oauthswift = OAuth1Swift(
                consumerKey:    CONSUMER_KEY,
                consumerSecret: CONSUMER_SECRET,
                requestTokenUrl: "https://api.twitter.com/oauth/request_token",
                authorizeUrl:    "https://api.twitter.com/oauth/authorize",
                accessTokenUrl:  "https://api.twitter.com/oauth/access_token"
            )
            print("AFTER OAUTHSWIFT")
            handle = oauthswift.authorize(
                withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
                success: { credential, response, parameters in
                    print("OAuthToken: (credential.oauthToken)")
                    print("OAuthSecret: (credential.oauthTokenSecret)")
                    print("User ID: (parameters["user_id"]!)")
                    // Do your request
            },
                failure: { error in
                    print(error.localizedDescription)
                    print(self.handle)
            }
            )
            // Do any additional setup after loading the view, typically from a nib.
        }
    

  2. This is not the answer to your question.
    But try using the Fabric for install TwitterKit.
    For me personally this is an easier way.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search