Spotify: Login using the web api not accepting red

2019-05-06 18:03发布

I am trying to login to Spotify using the Web API since I don't need a session object. In my authorize method, I need to pass in the redirect url, but the way that the iOS redirect url is formatted is not accepted in a .GET request.

func authorize() {
    // create the url
    let url = "https://accounts.spotify.com/authorize"

    // parameters
    let parameters = ["client_id" : kClientID,
                      "response_type" : "code",
                      "redirect_uri" : "spotify-discover-login://callback",
                      "state" : kState,
                      "scope" : kScopes]

    // response code
    var responseCode = 401

    Alamofire.request(.GET, url, parameters: parameters, headers: nil)
        .responseString{response in
            print(response)
            switch response.result {
            case .Success:
                if let response = response.response {
                    responseCode = response.statusCode
                }
            case .Failure:
                print("fail")
                return
            }

            switch responseCode {
            case 200:
                print("200")
            case 202:
                print("@ACCEPTED")
            case 400:
                print("@BAD REQUEST")
            case 401:
                print("@AUTH FAIL")
            case 403:
                print("@FORBIDDEN")
            case 1004:
                print("@COULD NOT CONNECT")
            default: break
            }
    }
}

UPDATE:

This is the error that Xcode gives me:

FAILURE: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo={NSUnderlyingError=0x7fc89b4677a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=spotify-discover-login://callback/?code=AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXsSP8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA&state=random-string-state, NSErrorFailingURLKey=spotify-discover-login://callback/?code=AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXsSP8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA&state=random-string-state, NSLocalizedDescription=unsupported URL}

3条回答
爷的心禁止访问
2楼-- · 2019-05-06 18:45
手持菜刀,她持情操
3楼-- · 2019-05-06 19:01

This happened also when you dont make this in your info.plist file:

Steps to solve this issue:

  • Go to Project Navigato and search for info.plist file
  • Added a Key called NSAppTransportSecurity as a Dictionary.
  • Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES

Finally you must have:

enter image description here

查看更多
手持菜刀,她持情操
4楼-- · 2019-05-06 19:04

The NSURLErrorDomain code -1002 points to an NSURLErrorUnsupportedURL error. According to Apple, this error means:

NSURLErrorUnsupportedURL

Returned when a properly formed URL cannot be handled by the framework.

The most likely cause is that there is no available protocol handler for the URL.

Available in iOS 2.0 and later.

According to NSHipster, this error means:

"The connection failed due to an unsupported URL scheme."

So your URL is properly formed, but there's no protocol handler that knows what to do with the "spotify-discover-login" protocol.

But of course that's your custom URL scheme. Make sure you've properly registered your custom URL scheme in your info.plist and implemented application:openURL:options:. See this tutorial.

As a side note, your custom URL scheme name is quite generic. Another app or the Spotify app itself might use the exact same scheme either now or in the future, which could create a headache for you in debugging. Make sure to make the scheme unique, most likely including the name of your app or your company in the scheme, such as "PoKoBros-spotify-discover-login".

查看更多
登录 后发表回答