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

弯曲

我正在尝试使用 spotify web api 实现搜索目录请求。我已经按照 api 文档遵循了 http 请求格式,但是我收到了错误的请求 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 Params 具有“market” = “United States”,这是一个无效市场,您应该使用“US”作为该值。我测试了一下并message在返回的error“无效市场代码”

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python放置请求。Spotify API放置请求格式错误的Json

来自分类Dev

请求 Spotify 访问令牌时遇到问题 [错误请求]

来自分类Dev

Axios发布Spotify API请求返回400

来自分类Dev

Spotify请求的限制

来自分类Dev

Spotify 功能请求

来自分类Dev

在Express.js上使用Axios向Spotify API发出POST请求时出现错误400

来自分类Dev

Spotify WebAPI Url 请求语法

来自分类Dev

Spotify API 错误代码 400 - 无搜索查询

来自分类Dev

错误的请求错误 400

来自分类Dev

刷新令牌时,Spotify Web API错误请求错误“ invalid_client”

来自分类Dev

cURL请求Spotify API异常状态:415

来自分类Dev

Spotify dbus请求返回空元数据

来自分类Dev

cURL请求Spotify API异常状态:415

来自分类Dev

python请求400错误

来自分类Dev

$ .ajax 400错误请求

来自分类Dev

Angularjs错误请求(400)

来自分类Dev

HTTP请求:400错误

来自分类Dev

Ajax 400 错误请求

来自分类Dev

请求库的错误请求错误 400

来自分类Dev

AJAX错误:400错误的请求

来自分类Dev

烧瓶错误请求错误 400

来自分类Dev

AJAX请求的“ 400错误请求”响应

来自分类Dev

每个请求的Azure 400错误请求

来自分类Dev

卷曲SSL请求400错误请求

来自分类Dev

每个请求的Azure 400错误请求

来自分类Dev

Python POST 请求 400 错误请求

来自分类Dev

PHP fsockopen 400错误请求

来自分类Dev

文件上传-错误的请求(400)

来自分类Dev

调试400错误请求响应