我正在尝试向Imgur API发出HTTP请求。 我正在尝试检索与标签“cats”相关联的所有图像。 根据Imgur API,网址为:https://api.imgur.com/3/gallery/t/cats
Imgur API关于进行get请求所需授权的说明如下:
但是我一直得到403状态码,意味着需要授权。我做错了什么?
For public read-only and anonymous resources, such as getting image info, looking up user comments, etc. all you need to do is send an authorization header with your
client_id
in your requests. This also works if you'd like to upload images anonymously (without the image
being tied to an account), or if you'd like to create an anonymous
album. This lets us know which application is accessing the API.Authorization: Client-ID YOUR_CLIENT_ID
我已经查看了以下问题并尝试了其中提出的建议,但它们都没有帮助。
如何在Swift中进行Http get并设置httpHeader?
我的当前代码如下:
let string = "https://api.imgur.com/3/gallery/t/cats"
let url = NSURL(string: string)
let request = NSMutableURLRequest(URL: url!)
request.setValue("clientIDhere", forHTTPHeaderField: "Authorization")
//request.addValue("clientIDhere", forHTTPHeaderField: "Authorization")
request.HTTPMethod = "GET"
let session = NSURLSession.sharedSession()
let tache = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
if let antwort = response as? NSHTTPURLResponse {
let code = antwort.statusCode
print(code)
}
}
tache.resume()
但是我一直得到403状态码,意味着需要授权。我做错了什么?