Comments (14)
How I made it work :
-
Create a spotify app at https://developer.spotify.com/my-applications/#!/applications/create
-
Use the Client Credential workflow to get an api token from Spotify https://developer.spotify.com/web-api/authorization-guide/#client-credentials-flow
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.
from spotify-export.
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.
just noticed this gets resolved by #14
from spotify-export.
I'm getting it here too. git reset'ed (9/17/2017). Anyone found a solution for this? Thanks
from spotify-export.
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.
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.
Looks like it, yes. Did you try it out ?
from spotify-export.
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.
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.
It works, cheers!
from spotify-export.
@glfaulkner Yes it does.
from spotify-export.
from spotify-export.
@snakenerd works for me, I'm logging in with username & password if that helps.
from spotify-export.
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:
- Save the snippet above into a file called
fetch_token.js
. Fill in theclient_id
andclient_secret
. You don't have to encode them to base64 sinceBuffer
does it for you. - You can run it with, for example, nodejs:
node fetch_token.js
- It will return your with a token, you don't have to encode or decode it any further, just copy it.
- change te
lib/spotify-track.rb
as suggested above, fill in your copied token in the lineaccess_token = "..."
. - Run
./bin/spotify-export.rb your_playlist.text
from spotify-export.
Related Issues (18)
- undefined method `each' for nil:NilC HOT 5
- No RubyUser on Windows - Could not load sqlite_adapter HOT 2
- bundler/setup (LoadError) HOT 2
- No token provided HOT 1
- Activesupport requires Ruby>=2.2.2 HOT 1
- cannot load such file -- bundler/setup (LoadError) HOT 3
- Cannot find 'Copy Spotify URI' for bulk selection HOT 1
- `initialize': That file does not exist (SpotifyPlaylist::MissingFileError) HOT 1
- `format_artists': undefined method `each' HOT 1
- Help?
- New error?
- API to update HOT 2
- Question: is there a way to automate the copy/paste of playlists? HOT 1
- maximum spotify api requets HOT 4
- Not a ruby user - how to rectify ? HOT 3
- Doesn't appear to be support for web proxies HOT 2
- Missing requirement for devkit HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spotify-export.