【问题标题】:How to add Spotify Token to Search artist's track如何将 Spotify 令牌添加到搜索艺术家的曲目
【发布时间】:2019-07-04 13:25:16
【问题描述】:

尝试通过spotify web api 搜索艺术家的曲目。但是Spotify 已经更新了它的 api,现在需要一个 Token 来访问它。我必须标记,但找不到任何关于如何通过 Alamofire 使用它的方法。这是代码。任何帮助将不胜感激,或指出正确的方向。

class TableViewController: UITableViewController {

    var names = [String]()

    var searchURL = "https://api.spotify.com/v1/search?q=Odesza&type=track"

    typealias JSONStandard = [String : AnyObject]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        callAlamo(url: searchURL)
    }

    func callAlamo(url : String){
        AF.request(url).responseJSON(completionHandler: {
            response in
            self.parseData(JSONData: response.data!)
        })

    }
    func parseData(JSONData : Data) {
        do {
            var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        print(readableJSON)
            if let tracks = readableJSON["tracks"] as? JSONStandard{
               // print(readableJSON)

                if let items = tracks["items"] as? NSArray{

                    for i in 0..<items.count{
                        let item = items[i] as! JSONStandard

                        let name = item["name"] as! String
                        names.append(name)

                        self.tableView.reloadData()

                    }
                }
            }
        }
        catch{
            print(error)
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
       cell?.textLabel?.text = names[indexPath.row]
        return cell!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

【问题讨论】:

    标签: swift alamofire spotify


    【解决方案1】:

    您可以像这样在标头中设置访问令牌。有关详细信息和示例,您可以查看offical reference。顺便说一下this question的可能重复

       let headers = [
        "Authorization": "Bearer {your access token}"
    ]
    
    AF.request(.GET, searchURL, headers: headers)
             .responseJSON { response in
                 debugPrint(response)
             }
    

    【讨论】:

    • 所以当我尝试使用新的 AF.request 时,它只允许我在调用中使用一个参数,当我查找 a​​lamofire 请求参数时,它也只有一个参数。我将如何使用 .get 方法进行 spotify,然后在有或没有 AF.request 的情况下提交 searchURL 和标题?
    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2012-11-21
    • 2016-09-02
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多