Swift中的Spotfiy API-身份验证令牌

时间:2019-03-20 17:50:55

标签: swift xcode spotify

我正在尝试创建一个简单的搜索,用户可以在其中使用Spotify搜索音乐。我在从Spotify接收数据时遇到问题。正如您在下面看到的,我创建了一个简单的应用程序,该应用程序仅应打印出Spotify接收到的数据,而在下面则是控制台打印出的内容。有人告诉我auth可能设置不正确,但是我试图对其进行修复,但没有找到任何文档可以在xcode中实现它。

import UIKit
import Alamofire

class TableViewController: UITableViewController {
    
    var searchURL = "https://api.spotify.com/v1/search?q=tania%20bowra&type=track"
    let headers = ["Authorization": "Bearer{BQDrWHgHBM1wLztdtJowwbRaIs0HSrkBg4ZoNur5qTdf77mBnvWiqdNtlOnDM6lsdIpT1IA9uI0vqIo_7rILYWmGa6EQzm9Wd5YB6NJhKKftW3ZOjCkj_lL1B2s3L0wLVAKcyQiKja0}"]
    
    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){
        /*Alamofire.request(url).responseJSON(completionHandler: {
            response in
            
            self.parseData(JSONData: response.data!)
            
        })*/
        
        Alamofire.request(url, headers: headers).responseJSON {
            response in
            
            debugPrint(response)
            print("*************************")
            print(response.data!)
            
            self.parseData(JSONData: response.data!)
        }
        
    }
    func parseData(JSONData : Data){
        do{
            var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as? JSONStandard
            print(readableJSON)
        }
        catch{
            print("**************************************")
            print(CFCopyDescription)
        }
    }


}

[Request]: GET https://api.spotify.com/v1/search?q=tania%20bowra&type=track
[Response]: <NSHTTPURLResponse: 0x6000009d8300> { URL: https://api.spotify.com/v1/search?q=tania%20bowra&type=track } { Status Code: 400, Headers {
    "Access-Control-Allow-Origin" =     (
        "*"
    );
    "Content-Encoding" =     (
        gzip
    );
    "Content-Length" =     (
        112
    );
    "Content-Type" =     (
        "application/json",
        "application/json"
    );
    Date =     (
        "Tue, 19 Mar 2019 20:17:46 GMT"
    );
    Via =     (
        "1.1 google"
    );
    "Www-Authenticate" =     (
        "Bearer realm=\"spotify\", error=\"invalid_request\", error_description=\"Only valid bearer authentication supported\""
    );
    "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
    );
} }
[Data]: 99 bytes
[Result]: SUCCESS: {
    error =     {
        message = "Only valid bearer authentication supported";
        status = 400;
    };
}
[Timeline]: Timeline: { "Request Start Time": 574719466.223, "Initial Response Time": 574719466.647, "Request Completed Time": 574719466.651, "Serialization Completed Time": 574719466.704, "Latency": 0.423 secs, "Request Duration": 0.428 secs, "Serialization Duration": 0.053 secs, "Total Duration": 0.481 secs }
*************************
99 bytes
Optional(["error": {
    message = "Only valid bearer authentication supported";
    status = 400;
}])

0 个答案:

没有答案
相关问题