0

Trying to search an artist's track through spotify web api. However Spotify has updated its api and now needs a Token to access this. I have to token but could not find anything of how to use this via Alamofire. Here is the code. Any help would be greatly appreciated, or being pointed in the right direction.

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.
    }
}
Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24

1 Answers1

2

You can set access token in headers like that.For detailed information and examples you can look at offical reference. By the way possible duplicate of this question

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

AF.request(.GET, searchURL, headers: headers)
         .responseJSON { response in
             debugPrint(response)
         }
Ahmet Sina Ustem
  • 1,090
  • 15
  • 32
  • So when i try to use the new AF.request it only allows me one argument in the call, when i looked up the alamofire request parameters it was only one agrument also. How would i use the .get method for spotify and then submit searchURL and headers with or without AF.request? – Patrick Katz Feb 13 '19 at 05:07