如何添加Spotify的令牌搜索艺术家曲目

问题描述 投票:0回答:1

试图通过spotify网络API搜索艺术家的轨道。然而Spotify已经更新了其API和现在需要一个令牌访问此。我有令牌,但无法找到如何通过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个回答
2
投票

你可以像that.For的详细信息和例子,你可以看看offical reference头设置访问令牌。通过this question的方式可能重复

   let headers = [
    "Authorization": "Bearer {your access token}"
]

AF.request(.GET, searchURL, headers: headers)
         .responseJSON { response in
             debugPrint(response)
         }
© www.soinside.com 2019 - 2024. All rights reserved.