Coder Social home page Coder Social logo

Comments (14)

BachirC avatar BachirC commented on July 18, 2024 11

How I made it work :

For this part, you will need to Base64 encode the string client_id:client_secret of your Spotify app. I used this https://www.base64encode.org/.
Also, beware to pass the grant_type params in the body and not as an URL-encoded param otherwise you will get a 400.

  • You get the access_token (expires in 1 hour) in the response. I added two lines to the code (last ones) :

lib/spotify-track.rb

  def get_track_attributes
    if local
      local_array = uri.split(':')

      # The array should be length 6
      # ["spotify", "local", "artist", "album", "song title", "duration"]
      name   = URI.decode(local_array[4].gsub('+', ' '))
      album  = URI.decode(local_array[3].gsub('+', ' '))
      artist = URI.decode(local_array[2].gsub('+', ' '))
    else
      track_id = uri[/track(:|\/)(.+)/, 2]
      target  = URI.parse("https://api.spotify.com/v1/tracks/#{ track_id }")
      http    = Net::HTTP.new(target.host, target.port)
      http.use_ssl = true
      request = Net::HTTP::Get.new(target.request_uri)
      access_token = "Zwejkrhqwlkrq..."
      request['Authorization'] = "Bearer #{access_token}"

If I have some time, I'll do a PR to allow a user to pass the access token directly in the args when launching the script (the access token generation will be done manually as I explained above). It is not optimal as the token will stay in your bash history but for now it'll do and I'm sure we'll come up with a better solution.
Hope this helps.

⚠️ Don't commit or share your Spotify token.

from spotify-export.

xaviarias avatar xaviarias commented on July 18, 2024 1

I have the same issue here, but In my case #14 does not solve the problem, it causes infinite recursion. The problem seems that artists parameter is null, not a typo.

from spotify-export.

OwenRay avatar OwenRay commented on July 18, 2024

just noticed this gets resolved by #14

from spotify-export.

fredericoabraham avatar fredericoabraham commented on July 18, 2024

I'm getting it here too. git reset'ed (9/17/2017). Anyone found a solution for this? Thanks

from spotify-export.

BachirC avatar BachirC commented on July 18, 2024

The problem is that the API call to Spotify in lib/spotify-track.rb is returning a 401 because no token is passed with the request. As stated by the API used in this gem, https://developer.spotify.com/web-api/get-track/ you will need to implement an Authorization workflow (https://developer.spotify.com/web-api/authorization-guide/) to generate a user token that will be passed in the requests to Spotify.

from spotify-export.

fredericoabraham avatar fredericoabraham commented on July 18, 2024

Hi. Still trying to make this work. Does the token look something like this after base 64 encode? (I messed with it so it's wrong):

  access_token = "YTU5YTc3AmVmADk4NDI2M2E2MGU2ZTU4NGFiYWYyM2Y="

Thanks.

from spotify-export.

BachirC avatar BachirC commented on July 18, 2024

Looks like it, yes. Did you try it out ?

from spotify-export.

BachirC avatar BachirC commented on July 18, 2024

Ok wait didn't read well. It's not the token that should be base64 encoded, it is the string client_id:client_secret.

Checkout the second link I posted in my workflow, it is well explained.

from spotify-export.

glfaulkner avatar glfaulkner commented on July 18, 2024

Still throwing me the same exception, even with adding the access token (which I obtained with curl passing the base64 string) to spotify-track.rb. When encoding the client id and secret to base64, does the string need to follow the exact 'client_id:client_secret" format, with the colon in the middle?

Thanks.

from spotify-export.

regisbsb avatar regisbsb commented on July 18, 2024

It works, cheers!

from spotify-export.

BachirC avatar BachirC commented on July 18, 2024

@glfaulkner Yes it does.

from spotify-export.

fredericoabraham avatar fredericoabraham commented on July 18, 2024

from spotify-export.

OwenRay avatar OwenRay commented on July 18, 2024

@snakenerd works for me, I'm logging in with username & password if that helps.

from spotify-export.

yijiangh avatar yijiangh commented on July 18, 2024

Complementing the awesome instructions above, for those who are as new to javascript as me, you need to run the following script (adapted from the example from Spotify that's not directly runnable 😂):

var request = require('request'); // "Request" library

var client_id = 'YOUR CLIENT ID';
var client_secret = 'YOUR CLIENT SCRET';

var authOptions = {
  url: 'https://accounts.spotify.com/api/token',
  headers: {
    'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
  },
  form: {
    grant_type: 'client_credentials'
  },
  json: true
};

request.post(authOptions, function(error, response, body) {
  if (!error && response.statusCode === 200) {
    var token = body.access_token;
    console.log(token);
  }
});

So the steps are:

  1. Save the snippet above into a file called fetch_token.js. Fill in the client_id and client_secret. You don't have to encode them to base64 since Buffer does it for you.
  2. You can run it with, for example, nodejs: node fetch_token.js
  3. It will return your with a token, you don't have to encode or decode it any further, just copy it.
  4. change te lib/spotify-track.rb as suggested above, fill in your copied token in the line access_token = "...".
  5. Run ./bin/spotify-export.rb your_playlist.text

from spotify-export.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.