如何使用 Spotify SDK 和 Swift 正确处理令牌刷新 3.错误代码=3840
How to properly handle token refresh with Spotify SDK and Swift 3. Error Code=3840
tl;dr I'm receiving: JSON text did not start with array or object and option to allow fragments not set.
if i'm trying to receive a token and No refresh token available in the session!
if I'm trying to renew a token.
我正在尝试为 Objective-C Spotify iOS SDK beta-25 在 Swift 中设置令牌刷新 3. 我正在使用 Heroku 服务器和 Ruby Spotify 提供的脚本,更改为我的凭据。
require 'sinatra'
require 'net/http'
require 'net/https'
require 'base64'
require 'encrypted_strings'
require 'json'
CLIENT_ID = ENV['xxx']
CLIENT_SECRET = ENV['xxx']
ENCRYPTION_SECRET = ENV['xxx']
CLIENT_CALLBACK_URL = ENV['xxx://returnafterlogin']
AUTH_HEADER = "Basic " + Base64.strict_encode64(CLIENT_ID + ":" + CLIENT_SECRET)
SPOTIFY_ACCOUNTS_ENDPOINT = URI.parse("https://accounts.spotify.com")
get '/' do
"Working"
end
post '/swap' do
# This call takes a single POST parameter, "code", which
# it combines with your client ID, secret and callback
# URL to get an OAuth token from the Spotify Auth Service,
# which it will pass back to the caller in a JSON payload.
auth_code = params[:code]
http = Net::HTTP.new(SPOTIFY_ACCOUNTS_ENDPOINT.host, SPOTIFY_ACCOUNTS_ENDPOINT.port)
http.use_ssl = true
request = Net::HTTP::Post.new("/api/token")
request.add_field("Authorization", AUTH_HEADER)
request.form_data = {
"grant_type" => "authorization_code",
"redirect_uri" => CLIENT_CALLBACK_URL,
"code" => auth_code
}
response = http.request(request)
# encrypt the refresh token before forwarding to the client
if response.code.to_i == 200
token_data = JSON.parse(response.body)
refresh_token = token_data["refresh_token"]
encrypted_token = refresh_token.encrypt(:symmetric, :password => ENCRYPTION_SECRET)
token_data["refresh_token"] = encrypted_token
response.body = JSON.dump(token_data)
end
status response.code.to_i
return response.body
end
post '/refresh' do
# Request a new access token using the POST:ed refresh token
http = Net::HTTP.new(SPOTIFY_ACCOUNTS_ENDPOINT.host, SPOTIFY_ACCOUNTS_ENDPOINT.port)
http.use_ssl = true
request = Net::HTTP::Post.new("/api/token")
request.add_field("Authorization", AUTH_HEADER)
encrypted_token = params[:refresh_token]
refresh_token = encrypted_token.decrypt(:symmetric, :password => ENCRYPTION_SECRET)
request.form_data = {
"grant_type" => "refresh_token",
"refresh_token" => refresh_token
}
response = http.request(request)
status response.code.to_i
return response.body
end
设置者:
SPTAuth.defaultInstance().tokenSwapURL = URL(string: SpotifyCredentials.tokenSwapURLSwap)
SPTAuth.defaultInstance().tokenRefreshURL = URL(string: SpotifyCredentials.tokenSwapURLRefresh)
现在用户无法再登录,我收到了顶部发布的错误。如果我要删除 tokenSwapURL
和 tokenRefreshURL
,一切都会恢复正常,但用户必须每 60 分钟重新授权一次。
如果我尝试使用已登录的用户刷新令牌,我会收到:
"No refresh token available in the session!"
if SPTAuth.defaultInstance().session != nil {
print("needs login")
SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: { error, session in
if error != nil {
print("\(error?.localizedDescription)") // "No refresh token available in the session!"
return
}
})
}
我错过了什么?非常感谢帮助。
简短摘要:假设您的 JSON 解析工作正常,问题是格式错误 JSON(服务器端)。
JSON text did not start with array or object and option to allow fragments not set
可以被的JSONSerialization.jsonObject(with:options:)
which returns
抛出
A Foundation object from the JSON data in data
, or
nil
if an error occurs.
格式错误 JSON » nil
而不是令牌 » 当前令牌被取消 » 结果:“用户无法再登录”
格式错误的可能解释 JSON 包括:
It usually is because of some warning message throwing out from your server without putting it in the response array. For example in PHP, some "warning messages" are not caught in your array so that when you finally use "echo json_encode($RESPONSE_ARR)," it is not a JSON format.
—
来自同一个 SO 页面:
You need to debug this in the iOS application. First convert the data to a string and print that and check it. If the string looks alright then print the data itself - sometimes people manage to add 0 bytes or control characters, or two byte order markers or something similar which are invisible in the string but are not legal JSON. —
我已经能够使用以下 Git 为 Spotify 创建令牌刷新服务:
您需要做的就是按照 git 项目中 Heroku link 的说明进行操作。
我试图与该项目的作者取得联系,但他无法告诉我,为什么我的方法行不通,而他的方法行得通。我能留给你的就是这个工作 Deploy to Heroku
link.
tl;dr I'm receiving:
JSON text did not start with array or object and option to allow fragments not set.
if i'm trying to receive a token andNo refresh token available in the session!
if I'm trying to renew a token.
我正在尝试为 Objective-C Spotify iOS SDK beta-25 在 Swift 中设置令牌刷新 3. 我正在使用 Heroku 服务器和 Ruby Spotify 提供的脚本,更改为我的凭据。
require 'sinatra'
require 'net/http'
require 'net/https'
require 'base64'
require 'encrypted_strings'
require 'json'
CLIENT_ID = ENV['xxx']
CLIENT_SECRET = ENV['xxx']
ENCRYPTION_SECRET = ENV['xxx']
CLIENT_CALLBACK_URL = ENV['xxx://returnafterlogin']
AUTH_HEADER = "Basic " + Base64.strict_encode64(CLIENT_ID + ":" + CLIENT_SECRET)
SPOTIFY_ACCOUNTS_ENDPOINT = URI.parse("https://accounts.spotify.com")
get '/' do
"Working"
end
post '/swap' do
# This call takes a single POST parameter, "code", which
# it combines with your client ID, secret and callback
# URL to get an OAuth token from the Spotify Auth Service,
# which it will pass back to the caller in a JSON payload.
auth_code = params[:code]
http = Net::HTTP.new(SPOTIFY_ACCOUNTS_ENDPOINT.host, SPOTIFY_ACCOUNTS_ENDPOINT.port)
http.use_ssl = true
request = Net::HTTP::Post.new("/api/token")
request.add_field("Authorization", AUTH_HEADER)
request.form_data = {
"grant_type" => "authorization_code",
"redirect_uri" => CLIENT_CALLBACK_URL,
"code" => auth_code
}
response = http.request(request)
# encrypt the refresh token before forwarding to the client
if response.code.to_i == 200
token_data = JSON.parse(response.body)
refresh_token = token_data["refresh_token"]
encrypted_token = refresh_token.encrypt(:symmetric, :password => ENCRYPTION_SECRET)
token_data["refresh_token"] = encrypted_token
response.body = JSON.dump(token_data)
end
status response.code.to_i
return response.body
end
post '/refresh' do
# Request a new access token using the POST:ed refresh token
http = Net::HTTP.new(SPOTIFY_ACCOUNTS_ENDPOINT.host, SPOTIFY_ACCOUNTS_ENDPOINT.port)
http.use_ssl = true
request = Net::HTTP::Post.new("/api/token")
request.add_field("Authorization", AUTH_HEADER)
encrypted_token = params[:refresh_token]
refresh_token = encrypted_token.decrypt(:symmetric, :password => ENCRYPTION_SECRET)
request.form_data = {
"grant_type" => "refresh_token",
"refresh_token" => refresh_token
}
response = http.request(request)
status response.code.to_i
return response.body
end
设置者:
SPTAuth.defaultInstance().tokenSwapURL = URL(string: SpotifyCredentials.tokenSwapURLSwap)
SPTAuth.defaultInstance().tokenRefreshURL = URL(string: SpotifyCredentials.tokenSwapURLRefresh)
现在用户无法再登录,我收到了顶部发布的错误。如果我要删除 tokenSwapURL
和 tokenRefreshURL
,一切都会恢复正常,但用户必须每 60 分钟重新授权一次。
如果我尝试使用已登录的用户刷新令牌,我会收到:
"No refresh token available in the session!"
if SPTAuth.defaultInstance().session != nil {
print("needs login")
SPTAuth.defaultInstance().renewSession(SPTAuth.defaultInstance().session, callback: { error, session in
if error != nil {
print("\(error?.localizedDescription)") // "No refresh token available in the session!"
return
}
})
}
我错过了什么?非常感谢帮助。
简短摘要:假设您的 JSON 解析工作正常,问题是格式错误 JSON(服务器端)。
JSON text did not start with array or object and option to allow fragments not set
可以被的JSONSerialization.jsonObject(with:options:)
which returns
A Foundation object from the JSON data in
data
, or
nil
if an error occurs.
格式错误 JSON » nil
而不是令牌 » 当前令牌被取消 » 结果:“用户无法再登录”
格式错误的可能解释 JSON 包括:
It usually is because of some warning message throwing out from your server without putting it in the response array. For example in PHP, some "warning messages" are not caught in your array so that when you finally use "echo json_encode($RESPONSE_ARR)," it is not a JSON format. —
来自同一个 SO 页面:
You need to debug this in the iOS application. First convert the data to a string and print that and check it. If the string looks alright then print the data itself - sometimes people manage to add 0 bytes or control characters, or two byte order markers or something similar which are invisible in the string but are not legal JSON. —
我已经能够使用以下 Git 为 Spotify 创建令牌刷新服务:
您需要做的就是按照 git 项目中 Heroku link 的说明进行操作。
我试图与该项目的作者取得联系,但他无法告诉我,为什么我的方法行不通,而他的方法行得通。我能留给你的就是这个工作 Deploy to Heroku
link.