Skip to content

Instantly share code, notes, and snippets.

@georgi
Created March 8, 2012 12:37

Revisions

  1. Matthias Georgi revised this gist Mar 8, 2012. 4 changed files with 34 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions client-side-authentication.rb
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,12 @@
    user = User.from_facebook(client)
    session[:user] = user.uid
    redirect '/'
    end

    class Facebook
    # Refresh short-lived token with a long-lived one
    def self.exchange_token(token)
    res = access_token(:grant_type => "fb_exchange_token", :fb_exchange_token => token)
    new(parse_token(res.body))
    end
    end
    1 change: 1 addition & 0 deletions javascript-auth.js
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,7 @@ $(function() {
    // The user hasn't connected yet, so we redirect to the
    // Facebook Authentication Dialog.
    window.location = App.authUrl;
    // https://www.facebook.com/dialog/oauth?scope=user_actions:soundcloud&redirect_uri=https://mobile-hack.herokuapp.com/auth
    }
    });
    }
    8 changes: 8 additions & 0 deletions server-side-authentication.rb
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,12 @@
    user = User.from_facebook(client)
    session[:user] = user.uid
    redirect '/'
    end

    class Facebook
    # Request access token for given authorization code
    def self.exchange_code(code, redirect_uri)
    res = access_token(:code => code, :redirect_uri => redirect_uri)
    new(parse_token(res.body))
    end
    end
    17 changes: 17 additions & 0 deletions soundcloud.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    class Soundcloud
    APP_ID = 'YOUR_CLIENT_ID'

    def self.http(domain = "api.soundcloud.com")
    Net::HTTP.new(domain, 80)
    end

    def self.get(path, params={})
    JSON.parse(http.get(path + '.json?' + urlencode_hash({ :client_id => APP_ID }.merge(params))).body)
    end

    # Returns an embeddable player
    def self.oembed(url)
    JSON.parse(http('soundcloud.com').get('/oembed?' + urlencode_hash(:format => 'json', :url => url)).body)
    rescue JSON::ParserError
    end
    end
  2. Matthias Georgi revised this gist Mar 8, 2012. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions fetching-actions.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # Shows a list of recent plays on SoundCloud
    get "/" do
    begin
    if user
    # Logged in users have a Facebook connection
    @actions = user.facebook.get('/me/soundcloud:listen')['data']
    end

    # rendering html template
    erb :index

    # Access Token is expired, so we reauth the user on Facebook
    rescue Facebook::OAuthException
    redirect auth_url
    end
    end
  3. Matthias Georgi revised this gist Mar 8, 2012. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions javascript-auth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    $(function() {
    FB.init({
    appId : App.appId,
    cookie : true,
    oauth : true
    });

    // If user is not logged in, we try to authenticate from Facebook.
    if (!App.userId) {

    // Query Facebook API for authentication data
    FB.getLoginStatus(function(response) {

    // The user already connected to the app, so we just need to
    // send the access token to login the user.
    if (response.status === 'connected') {
    $('#access-token').val(response.authResponse.accessToken);
    $('#auth-form').submit();
    }
    else {
    // The user hasn't connected yet, so we redirect to the
    // Facebook Authentication Dialog.
    window.location = App.authUrl;
    }
    });
    }
    });
  4. Matthias Georgi revised this gist Mar 8, 2012. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions client-side-authentication.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Login endpoint for client side flow
    # Takes a token paremeter and creates a user if necessary
    post "/auth" do
    client = Facebook.exchange_token(params[:token])
    user = User.from_facebook(client)
    session[:user] = user.uid
    redirect '/'
    end
  5. Matthias Georgi renamed this gist Mar 8, 2012. 1 changed file with 0 additions and 0 deletions.
  6. Matthias Georgi revised this gist Mar 8, 2012. No changes.
  7. Matthias Georgi created this gist Mar 8, 2012.
    8 changes: 8 additions & 0 deletions Server side authentication
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Callback endpoint for server side flow
    # Takes a OAuth code parameter and creates a user if necessary
    get "/auth" do
    client = Facebook.exchange_code(params[:code], url('/auth'))
    user = User.from_facebook(client)
    session[:user] = user.uid
    redirect '/'
    end