This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Youtube playback
#19
While this can be implemented with manual copying of tokens this solution might stop working at any point. The problem is that OAuth is means for end-user apps with UI not for server-to-server communication (Spotify server-to-server API does not provide player control). You will need a script to refresh the token periodically as it has a short expiration time.

This example might be helpful. It's for different OAuth service but quite similar. You need to put refresh token into storage variable and use access token from storage in scripts that send API requests.
Code:
url = require('socket.url')
http = require('ssl.https')
ltn12 = require('ltn12')
json = require('json')

refresh_token = storage.get('oauth_refresh_token')
if not refresh_token then
  log('missing refresh_token')
  return
end

local function buildqs(chunks)
  local qs = {}
  for k, v in pairs(chunks) do
    qs[ #qs + 1 ] = url.escape(tostring(k)) .. '=' .. url.escape(tostring(v))
  end
  return table.concat(qs, '&')
end

payload = buildqs({
  grant_type = 'refresh_token',
  client_id = 'CLIENT_ID',
  client_secret = 'CLIENT_SECRET',
  refresh_token = refresh_token,
})

source = ltn12.source.string(payload)

headers = {
  ['content-length'] = tostring(#payload),
  ['content-type'] = 'application/x-www-form-urlencoded; charset=UTF-8',
}

sink, output = ltn12.sink.table()

local one, code, hdrs, status = http.request({
  url = 'ENDPOINT_URL',
  sink = sink,
  source = source,
  method = 'POST',
  headers = headers,
})

res = table.concat(output)

if code == 200 then
  res, err = json.pdecode(res)

  if type(res) == 'table' then
    storage.set('oauth_access_token', res.access_token)
    storage.set('oauth_refresh_token', res.refresh_token)
  else
    log('json decode failed', res, err)
  end
else
  log('error', code, res)
end
Reply


Messages In This Thread
Youtube playback - by Jayce - 28.07.2017, 10:40
RE: Youtube playback - by morak - 02.08.2017, 13:54
RE: Youtube playback - by admin - 08.08.2017, 12:55
RE: Youtube playback - by Hippolyte - 03.02.2018, 23:08
RE: Youtube playback - by edgars - 05.02.2018, 08:02
RE: Youtube playback - by Thomas_LV - 14.03.2018, 15:57
RE: Youtube playback - by admin - 15.03.2018, 14:44
RE: Youtube playback - by Thomas_LV - 16.03.2018, 06:47
RE: Youtube playback - by admin - 16.03.2018, 08:18
RE: Youtube playback - by Thomas_LV - 16.03.2018, 09:30
RE: Youtube playback - by DGrandes - 04.02.2020, 15:44
RE: Youtube playback - by Daniel - 04.02.2020, 15:56
RE: Youtube playback - by DGrandes - 04.02.2020, 16:04
RE: Youtube playback - by Daniel - 04.02.2020, 16:05
RE: Youtube playback - by DGrandes - 04.02.2020, 16:12
RE: Youtube playback - by Daniel - 04.02.2020, 16:15
RE: Youtube playback - by DGrandes - 04.02.2020, 16:24
RE: Youtube playback - by Daniel - 04.02.2020, 16:37
RE: Youtube playback - by admin - 05.02.2020, 07:52

Forum Jump: