Spotify 搜索目录请求:400 错误请求

Spotify Search catalog request : 400 Bad request

我正在尝试使用 Spotify 网络实现搜索目录请求 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
);
} })

您的 URL 参数具有 "market" = "United States",这是一个无效市场,您应该使用 "US" 作为该值。我对此进行了测试,error 中返回的 message 表示 "Invalid Market Code"