UPE T D E D ...起作用的功能! 我想将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"
}
}
答案 0 :(得分:1)
您在标题和网址之间混在一起,需要正确设置标题
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
// ...
答案 1 :(得分:1)
让我们说您有一个带有“ 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()
}
请记住,您将用您的GET地址替换google.com,并用您自己的api键值替换APIKey标头。 同样,这将打印出所有的JSON,就像在PostMan中一样。 如果这对您有用,那么我还有访问JSON对象的链接。