OAuthSwift / OAuthSwiftAlamofire

Utility method to sign Alamofire request
MIT License
104 stars 38 forks source link

Error 400 in Twitter API - Need help #17

Closed vinoth-madhavan closed 6 years ago

vinoth-madhavan commented 6 years ago

Kindly excuse me if this is not OAuthSwiftAlamofire issue. I'm trying to access Twitter API and getting Error 400. Below is my code. Any idea what I'm doing wrong here? OAuth URL Callback is successful. But when I try to access the API I'm getting 400. Thank you in advance.

OAuth Setup and Authorize:

func doAuthService() {
        currentParameters["consumerKey"] = "************"
        currentParameters["consumerSecret"] = "**************"
        let oauthswift = OAuth1Swift(
            consumerKey:     currentParameters["consumerKey"]!,
            consumerSecret:  currentParameters["consumerSecret"]!,
            requestTokenUrl: "https://api.twitter.com/oauth/request_token",
            authorizeUrl:    "https://api.twitter.com/oauth/authorize",
            accessTokenUrl:  "https://api.twitter.com/oauth/access_token"
        )
        oauthswift.authorizeURLHandler = self.getUrlHandler()
        oauthswift.authorize(
            withCallbackURL: URL(string: "https://tinyurl.com/q5cr9wz")!,
            success: { (credential, response, parameters) in
                print(response)       
        }) { (error) in
            print(error.description)
        }

    }

Function to test Twitter:

func testTwitter(){
        let headers: HTTPHeaders = [
            "Content-Type" : "application/json",
            "Accept": "application/json"
        ]
       let sessionManager = Alamofire.SessionManager.default
        sessionManager.adapter = OAuthSwiftRequestAdapter.init(oauthSwift!)
       sessionManager.request(URL(string: "https://api.twitter.com/1.1/statuses/mentions_timeline.json")!,
                              method: .get,
                              parameters: [:],
                              encoding: JSONEncoding.default,
                              headers: headers)
        .responseData { (response) in
            print(response)
        }
        }
}

Request HTTP Headers are:


▿ Optional<Dictionary<String, String>>
  ▿ some : 3 elements
    ▿ 0 : 2 elements
      - key : "Accept"
      - value : "application/json"
    ▿ 1 : 2 elements
      - key : "Content-Type"
      - value : "application/json"
    ▿ 2 : 2 elements
      - key : "Authorization"
      - value : "OAuth oauth_consumer_key=FN0mBsoNEBecDp1E7wxCHjmLa, oauth_nonce=F8813241, oauth_signature=vFliY51x1GuHyDL%2F8lmxLpx%2BL0E%3D, oauth_signature_method=HMAC-SHA1, oauth_timestamp=1531840882, oauth_token=6Jz9lwAAAAAA7yKpAAABZKjUp1o, oauth_version=1.0"