【问题标题】:Spotify Search catalog request : 400 Bad requestSpotify 搜索目录请求:400 错误请求
【发布时间】:2018-09-14 22:35:06
【问题描述】:

我正在尝试使用 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
);
} })

【问题讨论】:

    标签: ios swift spotify http-status-code-400


    【解决方案1】:

    您的 URL 参数的“市场”=“美国”是一个无效市场,您应该使用“美国”作为该值。我对此进行了测试,error 中返回的message 显示“无效的市场代码”

    【讨论】:

    • 非常感谢,成功了!不敢相信我在文档中错过了这一点。
    猜你喜欢
    • 2018-06-07
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多