更新...功能有效! 我想将 yelp api 合并到应用程序中,但无法成功在 URL 字符串上传递我的授权令牌。 我是否需要做一些事情来将
URLRequest
连接到 URLSessoin
调用并且不使用标头? 也许键值对是错误的?以下函数返回:
error = {
code = "TOKEN_MISSING";
description = "An access token must be supplied in order to use this endpoint.";
};
我能够使用邮递员让 yelp API 调用正常工作,但只能通过单击邮递员上的“标题”部分并输入 Bearer,然后输入我的 yelp 键。 我用 Google 搜索了一下,发现一些链接表明您可以向 URLSession 添加标头,我认为这可以像邮递员那样工作,但我无法让它工作。
我知道有一些带有 yelp API 存储库的 GitHub,但我试图不在我的应用程序中安装大量我不理解的代码,而我想要的只是我能看到的在邮递员上通过的 JSON。 谁能帮助我理解如何编辑类似于下面示例的代码,以便我可以获得 yelp 所需的授权/承载?
func getYelp() {
let appSecret = "Bearer <YELP APIKEY>"
let link = "https://api.yelp.com/v3/businesses/search?latitude=37.786882&longitude=-122.399972"
if let url = URL(string: link) {
// Set headers
var request = URLRequest(url: url)
request.setValue("Accept-Language", forHTTPHeaderField: "en-us")
request.setValue(appSecret, forHTTPHeaderField: "Authorization")
print("Attempting to get places around location from Yelp")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject // Added "as anyObject" to fix syntax error in Xcode 8 Beta 6
print("Printing all JSON/n/n//n--------------------------")
print(jsonResult)
print("Printing from results/n/n//n--------------------------")
if let description = ((jsonResult["search"] as? NSDictionary)?["context"] as? NSDictionary)?["href"] as? String {
} else {
print("JSON pull failed/n/n//n--------------------------")
}
} catch {
print("JSON Processing Failed/n/n//n--------------------------")
}
}
}
}
task.resume()
} else {
resultLabel.text = "Couldn't get results from Here"
}
}
您混淆了标头和网址,您需要正确设置标头
if let url = URL(string: "https://places.cit.api.here.com/places/v1/discover/around?at=37.776169%2C-122.421267&app_id=\(app_id)&app_code=\(app_code)") {
var request = URLRequest(url: url)
// Set headers
request.setValue("Accept-Language", forHTTPHeaderField: "en-us")
request.setValue("Authorization", forHTTPHeaderField: "Bearer " + token // Token here)
print("Attempting to get places around location")
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
// ...
假设您有一个带有“https://google.com”的API(这只是一个带有假密钥的示例) 和一个 api 密钥“ApiKey: 92927839238293d92d98d98d92”。
然后您将获取此信息并执行此操作。
let uri = URL(string:"https://google.com")
if let unwrappedURL = uri {
var request = URLRequest(url: unwrappedURL)request.addValue("92927839238293d92d98d98d92", forHTTPHeaderField: "ApiKey")
let dataTask = URLSession.shared.dataTask(with: request) { (data, response, error) in
// you should put in error handling code, too
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
// HERE'S WHERE YOUR DATA IS
print(json)
} catch {
print(error.localizedDescription)
}
}
}
dataTask.resume()
}
请记住,您需要将 google.com 替换为您的 GET 地址,并将 APIKey 标头替换为您自己的 api 键值。 此外,这将打印出所有 JSON,就像在 PostMan 中一样。 如果这对您有用,那么我还有一个有关访问 JSON 对象的链接。