How to consume the Spotify API in Ruby on rails 5.2

2

Working with the Spotify-API in Rails

I'm using

  • Rails 5.2.1.1
  • ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  • As of version 5.2 of Ruby on Rails, some improvements in the sections of security .
  • To connect the Spotify API with Rails, we suggest some Wrappers as it is guilhermesad / rspotify
  • Authentication of the application is necessary to achieve get the results of the API queries
  • Testing the API through RSspotify in the rails console throws me the following errors. (each line is an action)
  •   

    require 'rspotify'

         

    RSpotify::Track

         

    RSpotify::Track.search('bohemian')

         

    Throws me - > > RSpotify::MissingAuthentication (RSpotify::MissingAuthentication)

         

    RSpotify.authenticate("<spotify_client_id>", "<spotify_client_secret>")

         

    Throws me - > > RestClient::BadRequest (400 Bad Request)

    My question is, can you help me with the following?

    • How can I consume the spotify API in Ruby on Rails 5.2

    • How credentials are used in rails to consume this API

    • How can I authenticate from rails.

    Thank you in advance for the help.

        
    asked by vifrac 04.12.2018 в 06:20
    source

    1 answer

    1

    My questions were the following

    • How can I consume the spotify API in Ruby on Rails 5.2:

      • We can use the gem " rspotify " to consume Spotify data
    • How credentials are used in rails to consume this API:

      • To consume the Spotify data it is necessary to register the App that we developed, you can do it in the following link Spotify
      • When using Ruby on Rails 5.2, we have to add the access data of the app that we registered, to the file "../config/credentials.yml.enc" that means the "Client ID" and the "Client Secret"
      • To modify the file "../config/credentials.yml.enc", in the end (or command line) we must be located in the project directory and execute the command EDITOR=nano rails credentials:edit , then add the data of the «Client ID» and the «Client Secret»
      • It should look similar to the following

        development: spotify: access_key_id: abc123 secret_access_key: abc123

        production: spotify: access_key_id: abc123_ secret_access_key: abc123_

      • "../ config / master.key" must be added to "gitignore" as the decryption key

    • How can I authenticate from rails.

      • After uploading the access data to the corresponding file (credentials.yml.enc) we will create variables that allow us to read those data and upload them where we think necessary.
      • We should keep in mind that when we upload the app to a production environment, the decryption keys will not be loaded, therefore we must upload them manually or as suggested by the platform we use for that purpose, the following is an explanatory video for the platform heroku .
      • The variables would look similar to the following (the third line of this code is the way we authenticate ourselves in Spotify through "rspotify")

        s_id = Rails.application.credentials.development[:spotify][:access_key_id] s_secret = Rails.application.credentials.development[:spotify][:secret_access_key] RSpotify.authenticate(spotify_client_id.to_s, spotify_client_secret.to_s)

      

    I hope as well as this, this solution will also be of help to you.

        
    answered by 08.12.2018 / 00:58
    source