Guest User

Untitled

a guest
Jan 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. {"errors":[{"code":215,"message":"Bad Authentication data."}]}
  2.  
  3. func getRecentTweets(query : String)
  4. {
  5.  
  6. // The below are the oAuth Parameters for header
  7. let timeStamp = Date().toMillis()
  8. let oauthNonce = Utils.randomString(length: 32)
  9. let urlEncodedQuery = query.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  10. var paramterString = "q=(urlEncodedQuery)&result_type=recent&lang=en&oauth_consumer_key=(oauthConsumerKey)&oauth_nonce=(oauthNonce )&oauth_timestamp=(timeStamp ?? 0)&oauth_token=(oauthToken)&oauth_version=(oauthVersion)"
  11.  
  12. paramterString = paramterString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  13. var baseUrl = "https://api.twitter.com/1.1/search/tweets.json"
  14. baseUrl = baseUrl.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  15. let signature_base_string = "GET&(baseUrl)&(paramterString)"
  16. let consumerSecretEncoded = oauthConsumerSecret.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  17. let tokenSecretEncoded = oauthAccessSecret.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
  18. let signing_key = "(consumerSecretEncoded)&(tokenSecretEncoded)"
  19.  
  20. let oauthSignature = signature_base_string.hmac(key: signing_key)
  21.  
  22.  
  23.  
  24. let headers = [
  25. "Authorization": "OAuth oauth_consumer_key="(oauthConsumerKey)",oauth_token="(oauthToken)",oauth_signature_method="(oauthSignatureMethod)",oauth_timestamp="(timeStamp ?? 0)",oauth_nonce="(oauthNonce )",oauth_version="(oauthVersion)",oauth_signature="(oauthSignature)""
  26. ]
  27.  
  28. let request = NSMutableURLRequest(url: NSURL(string: "https://api.twitter.com/1.1/search/tweets.json?q=(urlEncodedQuery)&result_type=recent&lang=en")! as URL)
  29.  
  30. request.httpMethod = "GET"
  31. request.allHTTPHeaderFields = headers
  32. let session = URLSession.shared
  33. let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  34. if (error != nil) {
  35.  
  36. } else {
  37. self.parseData(data)
  38. }
  39. })
  40.  
  41. dataTask.resume()
  42. }
Add Comment
Please, Sign In to add comment