2017-09-22 12 views
0

のvarパラメータです:文字列Alamofire SWIFT 3.1郵便サービスコールの応答以下の私のコードは、文字列=

parameters["client_id"] = "trackmykid" 
    parameters["client_secret"] = "trackmykid" 
    parameters["grant_type"] = "password" 
    parameters["roleId"] = "2" 
    parameters["device_token"] = "12324567" 
    parameters["os_type"] = "ios" 
    parameters["username"] = username 
    parameters["password"] = password 

はヘッダを聞かせて:HTTPHeaders = [ "のContent-Type":「アプリケーション/ X- www-form-urlencodedで返します」、 "認可": "基本dHJhY2tteWtpZDp0cmFja215a2lk" ]

var Paramdict = [String: String]() 

    Paramdict = ["client_id":"trackmykid","client_secret":"trackmykid","grant_type":"password","roleId":"2","device_token":"123456","os_type":"ios", "username":username,"password":password] 


     Alamofire.request(Baseurl.appending("oauth/token"), method: .post, parameters: Paramdict as? Parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response:DataResponse<Any>) in 

     switch(response.result) { 
     case .success(_): 
      if response.result.value != nil{ 
       print(response.result.value ?? NSDictionary()) 

       onSuccess(response.result.value as! [AnyHashable : Any]) 

      } 
      break 

     case .failure(_): 

      print(response.result.error ?? NSString()) 

      onFailure(response.result.error!) 

      break 

     } 

応答として:

{error = "invalid_request"; "error_description" = "不足している許可タイプ"; }

+0

任意のクエリ私の答えをチェックし、私はあなたに –

答えて

0
let myParams = "UserName=\(txtemailaddress.text!)&Password=\(txtpassword.text!)&Grant_type=password&DeviceID=\(DEVICE_TOKEN as! String)&DeviceType=IOS" 
    let postData = myParams.data(using: String.Encoding.ascii, allowLossyConversion: true) 
    let postLength = String(format: "%d", postData!.count) 
    print(myParams) 
    let myRequest = NSMutableURLRequest(url: url) 
    myRequest.httpMethod = "POST" 
    myRequest.setValue(postLength, forHTTPHeaderField: "Content-Length") 
    myRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
    myRequest.httpBody = postData 

    let str = AppUtilities.sharedInstance.jsonToString(json: mainParam) 

Alamofire.request(myRequest as URLRequestConvertible) 
     .responseJSON { response in 
      // do whatever you want here 

      switch response.result { 
      case .success(let value): 
       let result = Result.success(value) 
       print(result) 
       let json = try? JSONSerialization.jsonObject(with: response.data!, options: []) 
       let Response : NSDictionary = json as! NSDictionary 
       print(Response) 

       if Response.value(forKey: "success") as! String == "1"{ 
        // GOT RESPONSE SUCCESS 
       } 
       else{ 
        KRProgressHUD.dismiss() 
        AppUtilities.sharedInstance.showAlert(title: "Error", msg: Response.value(forKey: "message") as! NSString) 
       } 

       break 
      case .failure: 
       let result = Result<Data>.failure 

       KRProgressHUD.dismiss() 
       print("exception: \(response.result.error)") 

       break 
      } 
    } 

コードについてはJSON文字列にParamの変換

func jsonToString(json: AnyObject) -> String{ 
     do { 
      let data1 = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data 
      let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string 
      print(convertedString!) // <-- here is ur string 
      return convertedString! 
     } catch let myJSONError { 
      print(myJSONError) 
      return "" 
     } 
    } 
+0

を助ける伝える:)答えるためにあなたに@Himanshu Moradiyaありがとうございます。あなたのコード行に、同じエラーとして結果が得られました。{ error = "invalid_request"; "error_description" = "不足している許可の種類";正ベアラトークンとして応答し、すべての – Narasimha

+0

とパラメータ – Narasimha

+0

@Narasimha私はあまりにもそのようにしようとしたヘッダフィールド –

関連する問題