-
-
Save kgrz/4523645 to your computer and use it in GitHub Desktop.
Soundcloud Sinatra authorization example. Created the anonymous gist by mistake and hence, forked it here. This is an example to explain the auth-flow and probably has bugs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Soundcloud, after authorization, redirects to a pre-registered URL | |
# with the user's code, access_token embedded in the URL which may | |
# be an issue! | |
# Alternatively, modify the #authorize_url() method such that the url | |
# that gets generated has a parameter "response_type" of "code" instead | |
# of "code_and_token" which is the default. | |
require 'sinatra' | |
require 'soundcloud' | |
class App < Sinatra::Base | |
def client_id | |
ENV['CLIENT_ID'] | |
end | |
def client_secret | |
ENV['CLIENT_SECRET'] | |
end | |
set :port, 9292 | |
get '/login' do | |
client = Soundcloud.new(:client_id => client_id, | |
:client_secret => client_secret, | |
:redirect_uri => "http://localhost:9292/done") | |
redirect client.authorize_url() | |
# Alternatively, the following can be used to control what you get out of | |
# the response from soundcloud | |
# auth_url = client.authorize_url() | |
# auth_url.gsub!("response_type=code_and_token", "response_type=code") To get only the code | |
# auth_url.gsub!("response_type=code_and_token", "response_type="token") to get the access_token of user | |
# redirect auth_url | |
end | |
get '/done' do | |
# the params hash would contain a "code" that is required to generate the | |
# access_token which is required for further requests to access the user's | |
# profile etc. | |
params | |
# user_token = client.exchange_token(:code => params[:code]) | |
# Tokens.save! user_token | |
# optionally, redirect to /me or / | |
# redirect '/' | |
end | |
run! | |
end | |
# Soundcloud embeds the Access code of the user who has logged in the query params. It can be obtained | |
# using params[:code] inside the '/done/ url. This code needs to be sent in all future requests to access | |
# the user's functions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment