Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reddit API的OAuth2错误-Swift_Swift_Post_Oauth_Alamofire_Reddit - Fatal编程技术网

Reddit API的OAuth2错误-Swift

Reddit API的OAuth2错误-Swift,swift,post,oauth,alamofire,reddit,Swift,Post,Oauth,Alamofire,Reddit,我正在将Reddit的API集成到我的iOS应用程序中,但是我遇到了一些查询问题。我正在使用Alamofire进行POST请求。当您在已安装的应用程序上使用Reddit的API时,您必须遵循其API。一切正常,但我返回了一个不支持的\u grant\u type错误 这是我的发帖请求 guard let url = URL(string: "https://www.reddit.com/api/v1/access_token") else { return false }

我正在将Reddit的API集成到我的iOS应用程序中,但是我遇到了一些查询问题。我正在使用Alamofire进行POST请求。当您在已安装的应用程序上使用Reddit的API时,您必须遵循其API。一切正常,但我返回了一个
不支持的\u grant\u type
错误

这是我的发帖请求

guard let url = URL(string: "https://www.reddit.com/api/v1/access_token") else { return false }
            let params: Parameters = [
                "grant_type" : "https://oauth.reddit.com/grants/installed_client",
                "device_id" : "\(UUID().uuidString)"]

            let username = "MY_CLIENT_ID"
            let password = ""
            let loginString = String(format: "%@:%@", username, password)
            let loginData = loginString.data(using: String.Encoding.utf8)! as NSData
            let base64EncodedString = loginData.base64EncodedString()

            let headers = ["Content-Type" : "application/x-www-form-urlencoded",
                           "Authorization" : "Basic \(base64EncodedString)"]

            Alamofire.request(url, method: .post , parameters: params, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
                print(response)

            }

我的
grant\u type
参数是否正确?正如我所相信的,因为文档告诉我要使用
https://oauth.reddit.com/grants/installed_client
作为已安装应用程序的授权类型。

这对我有效,但不使用AlamoFire tho

Swift 4.2

    let ACCESS_TOKEN_URL = "https://www.reddit.com/api/v1/access_token"
    let GRANT_TYPE = "https://oauth.reddit.com/grants/installed_client"
    let DONT_TRACK = "DO_NOT_TRACK_THIS_DEVICE"
    let timeout = 15
    let uncodedClientIDAndPassword = "\(CLIENT_ID):"
    let encodedClientIDAndPassword = uncodedClientIDAndPassword.toBase64()
    let authStr = "Basic \(encodedClientIDAndPassword!)"
    guard let serviceUrl = URL(string: ACCESS_TOKEN_URL) else {
        completionHandler(nil)
        return
    }
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.addValue(authStr, forHTTPHeaderField: "Authorization")

// Important Edit: You need to manually make the parameters, don't make a String dictionary and convert it to JSON and then sending it. Reddit doesn't accept that.
            let param = "grant_type=\(GRANT_TYPE)&device_id=\(DONT_TRACK)"
            let data = param.data(using: .utf8)
            request.httpBody = data
            // timeout
            let config = URLSessionConfiguration.default
            config.timeoutIntervalForRequest = TimeInterval(timeout)
            config.timeoutIntervalForResource = TimeInterval(timeout)
            let session = URLSession(configuration: config)

            session.dataTask(with: request) { (data, response, error) in
                if data != nil {
                    do {
                        if let json = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] {
                            self.accessToken = json["access_token"] as? String
                        } else {
                            self.accessToken = nil
                        }
                    }
                }
                completionHandler(self.getAccessToken())
            }.resume()
        }