PredictHQ API [“错误”:“ invalid_client]”身份验证“请求上的”错误请求“响应

时间:2019-05-27 07:58:51

标签: ios swift

当前希望使用Alamofire在PredictHQ API上进行身份验证请求。

我已经在开发人员控制台中生成了客户端凭据,并将其转移到我的项目中。问题出在我在函数* receiveJSONData()中使用URL https://api.predicthq.com/oauth2/token/接收请求的网络状态时。

下面是我在PredictHQ网站https://developer.predicthq.com/oauth2/上遵循的有关如何将cURL请求设置为格式的示例。

enter image description here enter image description here

  

1)我首先创建了networkServiceClient类

import UIKit
import Alamofire

class networkServiceClient {

    private let token = "GENERATED_TOKEN_FROM_PREDICTHQ_API".data(using: String.Encoding.utf8)!.base64EncodedString()
    private let secret = "GENERATED_SECRET_KEY_FROM_PREDICTHQ_API".data(using: String.Encoding.utf8)!.base64EncodedString()
    private let id = "GENERATED_ID_FROM_PREDICTHQ_API".data(using: String.Encoding.utf8)!.base64EncodedString()
    private let contentType = "application/json"
    private var authURL = URL(string: "https://api.predicthq.com/oauth2/token/")
    private var tokenDataURL = URL(string: "https://api.predicthq.com/v1/events/")

    typealias webResponse = ([[String: Any]]?, Error?) -> Void

    func receiveJSONData(completion: @escaping webResponse){

        let loginString = String(format: "Basic %@:%@", id, secret)

        let postHeaders:HTTPHeaders =  ["Authorization": loginString, "Accept": contentType]

        let params: [String : Any] = ["grant_type": "client_credentials", "scope": "account events signals"]

         //post authentication request
        AF.request(authURL!, method: .post, parameters: params, headers: postHeaders).responseJSON { (response) in

            if let error = response.error {

                completion(nil, error)

            } else if let jsonArray = response.value as? [[String: Any]] {

                completion(jsonArray, nil)

            } else if let jsonDict = response.value as? [String: Any] {

                completion([jsonDict], nil)

            }

        }

}
  

2)然后在我的TestingViewingController中实现了它

import UIKit
import Alamofire


class TestingViewController: UIViewController {

    private let networkingClient = networkServiceClient()
    override func viewDidLoad() {
        super.viewDidLoad()
        networkingClient.receiveJSONData() { (json, error) in
            if let error = error {
                print(error.localizedDescription)
            } else if let json = json {

                print(json.description)
            }

        }

    }

}
  

3)最后,这是我在TestingViewController控制台中的输出。

"error": invalid_client

enter image description here

1 个答案:

答案 0 :(得分:1)

嘿,可能是因为您正在分别对client_id和secret进行base64编码,但是如屏幕截图所示,它们应该一起编码(用冒号分隔)。