pilcrowonpaper/arctic

Allow client credentials grant type for spotify oauth

ridhwaans opened this issue · 1 comments

Currently the arctic spotify oauth provider establishes a token within the context of a user

Let it create auth with client_credentials flow
This bypasses the requirement for a redirect uri

example:

const axios = require('axios');
const qs = require('qs');
const Cookies = require('universal-cookie');
const cookies = new Cookies();

async function getAuthorizationToken() {
  return axios
    .post(
      'https://accounts.spotify.com/api/token',
      qs.stringify({
        grant_type: 'client_credentials',
        client_id: process.env.SPOTIFY_CLIENT_ID,
        client_secret: process.env.SPOTIFY_CLIENT_SECRET,
      }),
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      }
    )
    .then(function (response) {
      cookies.set('auth', response.data.access_token, {
        maxAge: response.data.expires_in,
      });
    });
}

const getAuth = async () => {
  let auth = cookies.get('auth');

  if (!auth) {
    await getAuthorizationToken();
    auth = cookies.get('auth');
  }

  return auth;
};

let access_token = await getAuth(); // authorization header

source:
https://developer.spotify.com/documentation/web-api/tutorials/client-credentials-flow
https://oauth.net/2/grant-types/client-credentials/

We don't plan to support grant types other than authorization code.