我正在尝试使用Spotify Web API实施搜索目录请求。我已经按照api文档的要求使用了http请求格式,但是收到了Bad Request 400响应。这是我的代码。谁能指出这里出什么问题了?
创建请求的函数:
static func createSpotifySearchRequest(with term: String, developerToken: String) -> URLRequest {
// Create the URL components for the network call.
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.spotify.com"
urlComponents.path = "/v1/search"
print(urlComponents.host)
let expectedTerms = term.replacingOccurrences(of: " ", with: "+")
let urlParameters = ["offset": "5",
"limit": "10",
"market": "United+States",
"type": "track",
"q": expectedTerms]
urlComponents.queryItems = [URLQueryItem(name: "q", value: "Hotel+California"), URLQueryItem(name: "type", value: "track"), URLQueryItem(name: "market", value: "United+States"), URLQueryItem(name: "limit", value: "10"), URLQueryItem(name: "offset", value: "5")]
// Create and configure the `URLRequest`.
print("in creation spotify")
var urlRequest = URLRequest(url: urlComponents.url!)
urlRequest.httpMethod = "GET"
print(urlRequest)
urlRequest.addValue("Bearer \(developerToken)", forHTTPHeaderField: "Authorization")
print(urlRequest)
print (urlRequest.allHTTPHeaderFields)
print("exiting creation spotify")
return urlRequest
}
调用函数:
let developerToken = (self.userDefaults.object(forKey: "Spotify_access_token")) as! String
let urlRequest = createSpotifySearchRequest(with: term, developerToken: developerToken)
//print(urlRequest)
//print (developerToken)
let task = urlSession.dataTask(with: urlRequest) { (data, response, error) in
//print(error)
print(response)
guard error == nil, let urlResponse = response as? HTTPURLResponse, urlResponse.statusCode == 200 else {
completion([], error)
//print("error response")
//print(error)
return
}
//print(urlResponse)
//print("response should be above this guy")
do {
let spotifyMediaItems = try self.processSpotifyMediaItemSections(from: data!)
completion(spotifyMediaItems, nil)
print(spotifyMediaItems)
} catch {
fatalError("An error occurred: \(error.localizedDescription)")
}
}
task.resume()
调试日志:
in creation spotify
https://api.spotify.com/v1/search?q=Hotel+California&type=track&market=United+States&limit=10&offset=5
https://api.spotify.com/v1/search?q=Hotel+California&type=track&market=United+States&limit=10&offset=5
Optional(["Authorization": "Bearer BQC4ZpVXj97*****************Y77yHzYR6HnxnxAiOPwVLA"])
exiting creation spotify
Optional(<NSHTTPURLResponse: 0x106a55f70> { URL: https://api.spotify.com/v1/search?q=Hotel+California&type=track&market=United+States&limit=10&offset=5 } { Status Code: 400, Headers {
"Access-Control-Allow-Origin" = (
"*"
);
"Cache-Control" = (
"private, max-age=0"
);
"Content-Encoding" = (
gzip
);
"Content-Length" = (
93
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Fri, 14 Sep 2018 22:18:31 GMT"
);
Via = (
"1.1 google"
);
"access-control-allow-credentials" = (
true
);
"access-control-allow-headers" = (
"Accept, Authorization, Origin, Content-Type, Retry-After"
);
"access-control-allow-methods" = (
"GET, POST, OPTIONS, PUT, DELETE, PATCH"
);
"access-control-max-age" = (
604800
);
"alt-svc" = (
clear
);
} })
答案 0 :(得分:1)
您的URL参数具有“市场” =“美国”的无效市场,您应该使用“美国”作为该值。我对此进行了测试,并且在message
中返回的error
表示“无效的市场代码”