即使我的令牌是正确的,我也会收到“缺少身份验证令牌”的输出消息

时间:2019-04-01 09:27:52

标签: json swift api

我正在使用glot.io的运行api为ipad创建一个在线编译应用程序,我从那里获取了令牌,并以他们指导的方式仍然以

的形式获取JSON消息。
{
    "message" : "Missing auth token"
} 

我正在快速开发应用程序,并使用Alamofire进行api调用。

let headers : [String : String] = [
    "Content-type": "application/json",
    "Authorization":"f49f383b-0710-494c-ad79-76cdf3970c53"
]

var body: [String : Any] = [
    "stdin": inputs,
    "files": [
        "name": "main.py",
        "content": content
    ]
]

Alamofire.request(
    "https://run.glot.io/languages/python/latest",
    method: .post,
    parameters: body,
    encoding: JSONEncoding.default,
    headers: headers
).responseJSON{(response) in
    if response.result.error == nil{
        guard let data = response.data else { return }
        do {
            let json = try JSON(data: data)
            print(json)
            outputcode = json["message"].stringValue

        }
        catch {
            debugPrint(error)
        }
    } else {
        debugPrint(response.result.error as Any)
    }
}

他们说要以这种方式实施。

curl --request POST \
 --header 'Authorization: Token 0123456-789a-bcde-f012-3456789abcde' \
 --header 'Content-type: application/json' \
 --data '{"files": [{"name": "main.py", "content": "print(42)"}]}' \
 --url 'https://run.glot.io/languages/python/latest'

1 个答案:

答案 0 :(得分:0)

像这样尝试一次::

    let headers: Dictionary = ["Accept":"application/json", "Content-type": "application/json", "Authorization":"Bearer f49f383b-0710-494c-ad79-76cdf3970c53"]

var params: [String : Any] = [
    "files": [
        "name": "main.py",
        "content": content
    ]
]

Alamofire.request(
    "https://run.glot.io/languages/python/latest",
    method: .post,
    parameters: params,
    encoding: JSONEncoding.default,
    headers: headers
).responseJSON{(response) in
    if response.result.error == nil{
        guard let data = response.data else { return }
        do {
            let json = try JSON(data: data)
            print(json)
            outputcode = json["message"].stringValue

        }
        catch {
            debugPrint(error)
        }
    } else {
        debugPrint(response.result.error as Any)
    }
}