Spotify:使用网页api登录不接受重定向网址 [英] Spotify: Login using the web api not accepting redirect url
问题描述
我尝试使用Web API登录Spotify,因为我不需要会话对象。在我的授权方法中,我需要传入重定向url,但iOS重定向url格式化的方式在.GET请求中不被接受。
func authorize(){
//创建url
let url =https://accounts.spotify.com/authorize
/ /参数
let parameters = [client_id:kClientID,
response_type:code,
redirect_uri:spotify-discover-login:// callback,
state:kState,
范围:kScopes]
//响应代码
var responseCode = 401
Alamofire.request .GET,url,parameters:parameters,headers:nil)
.responseString {$ b $ print(response)
switch response.result {$ b $ case .Success:
如果让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)
案例401:
print(@ AUTH FAIL)
案例403:
print(@ FORBIDDEN)
案例1004:
print(@ COULD NOT CONNECT)
default:break
}
}
}
$ b $这是Xcode给我的错误:
pFAILURE:错误域= NSURLErrorDomain代码= -1002不受支持的URL
UserInfo = {NSUnderlyingError = 0x7fc89b4677a0 {错误
域= kCFErrorDomainCFNetwork代码= -1002(null)},
NSError FailingURLStringKey = Spotify的,发现登录://回调/码= AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXsSP8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA和放大器;状态=随机字符串状态,
NSErrorFailingURLKey = Spotify的,发现登录://回调/?码= AQDKy5g8QOVodDd0kTEmqG-MXKdPmKiPzzSUSfZAY_Nh0J_SW8LYl7s583Pe6mu1kJcHA6Hyudpwhu-FkBXagvFE_Vh8ZVXsSP8sMZvJTikPkdJeV57vgJaL9f6K9QMLfGbIb1XuhqadLP30SGejyDoLGgVoLVtrW_ryWK4KQRwvQKNiitAW9kBDYry6A70i6R7aosFKOQrhswYxhH3Lre0ieBnCt0HrLozp3qQvnk36NKY2Ur2OdI92JOaf4Gk3UmLbrIyjcvUzdeK21tk-bkog9em0x3jJBKgeSAmiFz05ioehlboD9D79uvKPFfnA3hkvfBNFN5dvegiBcRfik7mNebckD2WRABqPyid5Xw8zt092sheCwhuxQDh13-LxGC4WfTlA5ydNrZlwQA5_5JcMQvgZZOA&安培;状态= random-string-state,
NSLocalizedDescription =不支持的URL}
NSURLErrorDomain
代码-1002指向 NSURLErrorUnsupportedURL
错误。根据
NSURLErrorUnsupportedURL
正确形成的URL不能被框架处理时返回。
最可能的原因是该URL没有可用的协议处理程序。
适用于iOS 2.0及更高版本。
根据 NSHipster ,此错误表示:
连接失败由于URL方案不受支持。
因此,您的网址已正确形成,但没有任何协议处理程序知道如何处理spotify-discover-login协议。
但当然这是您的自定义网址方案。确保你已经在你的info.plist中正确地注册了你的自定义URL方案,并实现了
application:openURL:options:
。请参阅此教程。
请注意,您的自定义网址方案名称非常通用。另一个应用程序或Spotify应用程序本身现在或将来都可能使用完全相同的方案,这可能会让您在调试时头疼。请务必使该方案具有独特性,最有可能包括您的应用或贵公司在该方案中的名称,例如PoKoBros-spotify-discover-login。
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}
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".
这篇关于Spotify:使用网页api登录不接受重定向网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!