2016-06-23 13 views
1

Ich kann nur mit Fabric autorisieren und userID und userName bekommen. Aber ich muss "accessToken" bekommen. Ich versuche, wie diese einfache Anfrage etwas senden:Wie bekomme ich Twitter Access Token mit Alamofire (Swift 2.2)

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

Aber ich war nicht erfolgreich (

Und noch ein ... Wie Recht auf ConsumerKey und ConsumerSecret als parametr passieren zu

Antwort

1

hier? ist die Antwort vielleicht hilft es jemand:

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)") 

      } 
} 
+0

Vielen Dank, Mann, das kommt in praktisch – rmvz3