如何使用 Alamofire 获取 Twitter 访问令牌 (Swift 2.2)

How to get Twitter access token with Alamofire (Swift 2.2)

我只能通过 Fabric 授权并获得 userIDuserName。但是我需要得到 "accessToken"。 我尝试发送这样的简单请求:

Alamofire.request(.GET, "https://api.twitter.com/1.1
/oauth2/token")
         .responseJSON { response in
             print(response)
         }

但是我没有成功(

还有一个...如何正确传递 ConsumerKeyConsumerSecret 作为参数?

这是答案,也许对某人有帮助:

let consumerKey = "yourConsumerKey"
let consumerSecret = "yourConsumerSecret"
let credentialsString = "\(consumerKey):\(consumerSecret)"
let credentialsData = (credentialsString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let base64String = credentialsData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64String)", "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"]
let params: [String : AnyObject] = ["grant_type": "client_credentials"]

Alamofire.request(.POST, "https://api.twitter.com/oauth2/token", headers: headers, parameters: params)
     .responseJSON { response in switch response.result {
         case .Success(let JSON):

            let response = JSON as! NSDictionary
            let userModel = response

            print("----------------")
            print("")
            print("userModel from TW")
            print(userModel)
            print("")
            print("----------------")

         case .Failure(let error):

            print("Request failed with error: \(error)")

            }
}