Giter Site home page Giter Site logo

facebook_oauth's Introduction

Facebook OAuth Graph API client library for Ruby

Install the gem

sudo gem install facebook_oauth

Using the gem

To make authorized requests with the client library you’ll need to create a Facebook Application.

See http://facebook-oauth.heroku.com/ for a full integration example.

Authorized request example

To use the full power of the Facebook API you need to authorize your application and a valid Facebook user via OAuth.
An example showing how to update the status of an authorized user is below.

Firstly we need to create an instance of the client with your application client credentials you have been given by Facebook
when you set up your application.

client = FacebookOAuth::Client.new(
    :application_id => 'YOUR_APPLICATION_ID',
    :application_secret => 'YOUR_APP_SECRET_KEY',
    :callback => 'http://example.com/facebook/callback'
)

client.authorize_url
=> "https://graph.facebook.com/oauth/authorize?scope=SCOPE&client_id=ID&type=web_server&redirect_uri=CALLBACK"

In your application your user would be redirected to Facebook to authorize the application at this point. The code continues below assuming the user has authorized your application, facebook will return to your callback URL with a code parameter.

access_token = client.authorize(:code => code)

client.me.info # returns your user information

Now if you keep hold of the access_token.token (usually in the database) for this user you won’t need to re-authorize them next time. When you create an instance of the client you can just pass in the access token that you have stored.

access_token = @user.access_token # assuming @user

client = FacebookOAuth::Client.new(
    :application_id => 'YOUR_APPLICATION_ID',
    :application_secret => 'YOUR_APP_SECRET_KEY',
    :token => access_token
)

client.me.info # returns your user information

Supported objects

  • Me (a special object that represents the current authorized user)
  • Album
  • Event
  • Group
  • Link
  • Note
  • Page
  • Photo
  • Post
  • Status
  • User
  • Video

You can access any object listed above in the same way via the API. For example:

client.me.home    # the authorized users news feed
client.event('event_id').attending    # event attendees
client.group('group_id').members    # group members
client.photo('photo_id').comments  # comments on a photo

Check out the Facebook API Reference to see what methods are available.

Publishing options

In order to publish content to a user you need to have the correct permissions. For example, to publish to a users wall you need the publish_stream permission. You specify the permissions you want to have for the authorizing user when you generate the authorize url like this:

client.authorize_url(:scope => 'publish_stream')

The scope is a comma-separated list of permissions. See the Facebook Permissions Documentation for more information.

With the correct permissions you can now post to a users wall like this:

client.me.feed(:create, :message => 'Testing writing to your wall...')

Other examples include:

client.event('event_id').attending(:create) # the user is attending the event
client.post('post_id').comments(:create, :message => 'my comment') # comment on a post
client.user('user_id').albums(:create, :name => 'my album', :message => 'cool pictures') # create an album

For a full list of supported publishing objects and parameters check out the Facebook API Documentation under “Publishing to Facebook.”

NOTE: I still have to implement the upload a photo method. Also if you can think of a better syntax for the create methods let me know!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.