Marcus
Marcus

Reputation: 9472

AlamoFire Authorization header Oauth

I'm trying to get tweets from a specific user using Alamofire and the twitter API. I have successfully made the call with a cURL command, but I can't make it with AlamoFire. It gives a 215 error for Bad Authentication data.

 curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json' --data 'count=2&screen_name=twitterapi' --header 'Authorization: OAuth oauth_consumer_key="lfa3XmdguuAqu3MoEQ6Fo01s0", oauth_nonce="0d50d7938a68be1ed73bcf02d4cc57e8", oauth_signature="8wZF1F%2B70okWLOi4s7dwKlJKc5w%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1460320508", oauth_version="1.0"' --verbose


        let key = "lfa3XmdguuAqu3MoEQ6Fo01s0"
    let secret = "25EUfqUstgdjZFgYnecrYuPkG6fDkruCjRXr905kM7475W2G63"
    let data = ["screen_name": "twitterapi", "count": 2]
    let path = "https://api.twitter.com/1.1/statuses/user_timeline.json"
    Alamofire.request(.GET, path, parameters: data)
        .authenticate(user: key, password: secret)
        .responseJSON { response in
            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
    }

How do I give the custom header Authorization in AlamoFire? I think I need an auth token, but I'm not sure where to start getting those. thanks

Upvotes: 1

Views: 1664

Answers (1)

Matheus Falcao
Matheus Falcao

Reputation: 29

To give a custom Authorization, you have to do this:

let headers = ["Authorization": "Bearer \(Token.sharedInstance.value!)"]

Alamofire.request(.PUT, url, headers: headers, parameters: data, encoding: .JSON).responseJSON { response in

}

Token Class:

class Token { 

    var value:String? 

    static let sharedInstance = Token() 

    private init() {      } 

}

Upvotes: 1

Related Questions