M.Singer
M.Singer

Reputation: 47

Spotify API Authorization Error (Swift)

I've made a little App in Swift where a user can search the Spotify database for songs. I am using the Web API Console > Search for an Item. My problem is the new OAuth system where you have to sign-in and all that stuff. My authorization is ok, but when I'm trying to get an access token with the following code, it's returning me the following error: {"error":"server_error","error_description":"Unexpected status: 400"}. My code is:

    let keys = "<MY_APPLICATION_KEYS>"

    let url = NSURL(string: "https://accounts.spotify.com/api/token")

    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url! as URL)

    request.httpMethod = "POST"

    request.setValue("Basic \(keys)", forHTTPHeaderField: "Authorization")

    request.setValue("client_credentials", forHTTPHeaderField: "grant_type")

    let task = session.dataTask(with: request as URLRequest) { (data, response, error) in

        guard let _: Data = data, let _: URLResponse = response, error == nil else {

            print(error!)
            return

        }

        let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("Data: \(dataString!)")

        self.parseData(JSONData: data!)

    }

    task.resume()

}

var accessToken = ""

func parseData(JSONData : Data) {

    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard

        if let token = readableJSON["access_token"] as? String {

            accessToken = token

        }

        print("Access Token: \(accessToken)")

        updateTokenInFirebase()

    }
    catch{
        print(error)
    }

Any help would be very appreciated, thank you very much in advance!

Documentation of the Web API: Web API Link

I am using on the Client Credentials Flow the first method.

Upvotes: 2

Views: 1111

Answers (2)

Peter Schorn
Peter Schorn

Reputation: 987

I know this is really late, but the issue is with this line:

request.setValue("client_credentials", forHTTPHeaderField: "grant_type")

According to the authorization guide, this should be in the body of the request, not the headers.

Upvotes: 0

Sami
Sami

Reputation: 587

I know it's been ~1 year since you posted this but I had the same issue and after a few tries was able to get it. You can test this in Playground.

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

if let url = URL(string: "https://accounts.spotify.com/api/token") {
    var postRequest = URLRequest(url: url)
    postRequest.httpMethod = "POST"
    let bodyParams = "grant_type=client_credentials"
    postRequest.httpBody = bodyParams.data(using: String.Encoding.ascii, allowLossyConversion: true)

    let id = "your client id"
    let secret = "your secret"
    let combined = "\(id):\(secret)"
    let combo = "\(id):\(secret)".toBase64()
    postRequest.addValue("Basic \(combo)", forHTTPHeaderField: "Authorization")

    let task = URLSession.shared.dataTask(with: postRequest) { (data, response, error) in
        guard let data = data else {
            return
        }
        print(String(data: data, encoding: String.Encoding.utf8)!)
    }
    task.resume()
}


extension String {

    func fromBase64() -> String? {
        guard let data = Data(base64Encoded: self) else {
            return nil
        }

        return String(data: data, encoding: .utf8)
    }

    func toBase64() -> String {
        return Data(self.utf8).base64EncodedString()
    } 
}

Upvotes: 1

Related Questions