MySpace Open Platform

A Place For Developers

Welcome Developers!

in

Welcome!

in

DA Rails w/ OAuth Sample Code

Last post 10-30-2009 1:02 PM by Kelly. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 08-02-2008 8:00 AM

    • Scott
    • Top 500 Contributor
    • Joined on 07-27-2008
    • Posts 13

    DA Rails w/ OAuth Sample Code

    Hi,

     I recently finished a VERY simple implementation of DA on Rails with the OAuth gem. I figured some people might be interested in some working code. To test, go to http://URL_TO_RAILS_APP/myspace and it will redirect you to authorize. Then, it will print the raw JSON to the screen. Note that the OAuth library does not currently support oauth_callback, and we must manually append it. The code below will run fine with the default routes.

    myspace_controller.rb

    require 'oauth/consumer'

    class MyspaceController < ApplicationController
        def index
            @consumer = OAuth::Consumer.new("CONSUMER_KEY","CONSUMER_SECRET", {
                    :http_method=>"get",
                    :site=>"http://api.myspace.com",
                    :request_token_path=>"/request_token",
                    :access_token_path=>"/access_token",
                    :authorize_path=>"/authorize"
            })
            @request_token=@consumer.get_request_token
            session[:request_token]=@request_token
            redirect_to @request_token.authorize_url+"&oauth_callback="+CGI.escape("http://URL_TO_RAILS_APP/myspace/callback")
        end
        
      def callback
            @request_token=session[:request_token]
            @access_token=@request_token.get_access_token
            @myspace_person=@access_token.get "/v2/people/@me/@self?format=json"
            @myspace_friends=@access_token.get "/v2/people/@me/@friends?format=json"
      end
    end 

    callback.html.erb

    <h1>Myspace#callback</h1>
    <p><%= @myspace_person.body %></p>
    <p><%= @myspace_friends.body %></p>
    <p><%= @myspace_activities.body %></p>

    Filed under:
  • 08-04-2008 4:21 PM In reply to

    Re: DA Rails w/ OAuth Sample Code

     Thanks! This is most helpful. To make the authorization persistent, how would you save @access_token? Would you just save the access token's string and secret?

  • 08-04-2008 11:35 PM In reply to

    • Scott
    • Top 500 Contributor
    • Joined on 07-27-2008
    • Posts 13

    Re: DA Rails w/ OAuth Sample Code

    coolchaser chao:

    To make the authorization persistent, how would you save @access_token? Would you just save the access token's string and secret?

    I am not sure that the access token is valid outside one given session... The documentation mentions a "session based Access Token", and tinkering also leads me to believe that it is only valid for the session.

     This begs the question - what can you do with data that is only retrievable one session and (for the most part) cannot be stored on the server? It seems very limiting.

  • 10-30-2009 1:02 PM In reply to

    • Kelly
    • Not Ranked
    • Joined on 10-30-2009
    • Posts 1

    Re: DA Rails w/ OAuth Sample Code

     Hi Scott,

     Thanks for the great example!  got this up and running in no time.  For other developers: if you run into the error:

    instance of OAuth::Consumer needs to have method `marshal_load' 

    check out this link:

    http://groups.google.com/group/oauth-ruby/browse_thread/thread/6658152e4333b2f3

Page 1 of 1 (4 items)