Giter VIP home page Giter VIP logo

facebook-twitter-posts-with-rails-example's Introduction

Facebook-Twitter-posts-with-rails-example

rails

This example is concentrating more on twitter and facebook integrations with rails.

Rails has many more gems to post, manage the social pages. I would like to discuss about facebook & twitter integrations.

Posting to facebook pages

For managing the pages and apps in facebook, we have to create an account in the facebook. Signup a facebook account with http://www.facebook.com

After creating the facebook account, create a page by using this link

   https://www.facebook.com/pages/create

alt create-page

we must login to facebook developers page to get access token

   https://developers.facebook.com

Facebook having graph explorer in facebook developers home page, from there you can get 2 hours acess token for facebook pages.

  • Select Application from the drop down in graph explorer tools, and select the get access token button, it prompt for permissions, select basic permissions and generate short access token.

       https://developers.facebook.com/tools/explorer

    alt 2-hours-fb-token

    alt fb-short-permissions

  • Facebook provides debug token explorer, it displays info about Access Token, Open Graph Id. more about Debugger Tool

    alt fb-debug-token

If you need an 60 days facebook page access token

  • We need to create an facebook app, for creating the app, go through -> Apps -> Create a New App in the facebook developers home page

    alt create-app alt create-app-dialog

  • In the process of managing pages through the app, we need App ID & App Secret.

    alt app-id

  • By using the AppID, make a request to following address

    note: The loggedin user must be manage his own pages through dialog box.

   https://www.facebook.com/dialog/pagetab?app_id=746040337858519&display=popup&next=http://www.facebook.com
  • It displays popup, select which page you want to manage it

    alt manage-page-apps

  • Then you make a request to get an 60 days token

       GET /oauth/access_token?
            grant_type=fb_exchange_token&
            client_id={app-id}&
            client_secret={app-secret}&
            fb_exchange_token={short-lived-token}
    • Example link:
       https://graph.facebook.com/oauth/access_token?client_id=749040378850589&client_secret=650d69481ea9deded7bddfc02816c2fb&grant_type=fb_exchange_token&fb_exchange_token=CAAKlnDxRGdcBAPFw25K59XoAF6ZBm3FpNAZABqI6mQYAudH8XCnt4ZCB0gN7vR3pahx25gu1jp9jARwfIqReUdBYfnPL8QNhso1neCfyQeRR0t4AElurhVhRo6o1bI1AFOGedb4JVDDZCCRDfBafZC6jEsqWZCdhrKTrzhpZCrBwqNkkRTNJ1RXStCnmIVfB0UZD

    Response should like this

      access_token=CAAKlnDxRGdcBAI4gH2wJaFwjeeqEn3m0v1vGRlT8sP6cUKKwGp5oTokKrR8NerUw4PRSbxQltvZB6bFZBCbeEu1l0VYeraYxMZB81y24AiEapoaw7URZBe4Pz6WGL1ZCfdA1ZC8ThTK112TRvbQv7EyuJdItXH1bKtcIy2lrIV8t4Lt7lZCEgiZA&expires=5162446

Using Koala gem to post facebook wall posts

  • Require koala gem into Gemfile

     gem 'koala'
  • By using the access token, we pick facebook graph object from koala

       #Picking the graph api object from kaola gem
       @graph = Koala::Facebook::API.new(@access_token)
  • In koala gem ,we have methods to manage posts in facebook. so below snippet is for wall post

       #put_wall_post is method to post an article to the pages
        post_info = @graph.put_wall_post(post_title, {
        name: link_name, description: post_description, picture: image_url, link: page_link
      })

Twetting to twitter pages with twitter client

  • At first, we have an twitter account to manage the tweets. for signup an account https://twitter.com/signup

  • Ruby twitter gem to manage twitter accounts, require in Gemfile

     gem 'twitter'
  • Managing the twitter accounts using OAuth based protocol, we need a privilliges to access twitter account, so for that we have to create an twitter app using twitter developers site

    Creating Twitter App

      https://apps.twitter.com/app/new
    
  • Displaying the apps

    https://apps.twitter.com/
    
  • Select an API Keys tab in home page of app, generate a new access token

  • Configure twitter client

      @client = Twitter::REST::Client.new do |config|
        config.consumer_key        = "YOUR_CONSUMER_KEY"
        config.consumer_secret     = "YOUR_CONSUMER_SECRET"
        config.access_token        = "YOUR_ACCESS_TOKEN"
        config.access_token_secret = "YOUR_ACCESS_SECRET"
      end
  • Using client object, post a tweet like this

      @client.update('First tweet')

    note: Twitter accept the tweet less than 140 characters, twitter need write permissioned access token for posting the tweet. for more about Twitter methods

Example App to Start Testing Facebook Post with Koala gem

  • We spend a lot of time on posting to facebook, We think to design an sample app to make it easier facebook integrations with rails.

Just Clone this example app

   git clone https://github.com/rdurgarao/facebook-twitter-posts-in-rails-example.git

Start the app using rails server

It started on rails 3000 port

rails server

Make a request to http://localhost:3000
  • Facebook wall Post

    • It displays a form with facebook fields as shown alt sample-wallpost-form

    • Place an access token in the input field, else configure it in facebook controller initializer block -> app -> controllers -> facebook_controller -> initialize block

       def initialize
            #specify access_token here
            @access_token = 'X'
       end
    • Post your article by clicking on post button

    • After succesfull posting an article, you get success response, else exception thrown by facebook or koala gem , More about exceptions

    • Finally, the posted article in the facebook.

    alt posted-article

  • Twitter tweet

    • Configure Twitter client in app -> controllers -> twitter_controller.rb -> initiliaze method config params

        @client = Twitter::REST::Client.new do |config|
              config.consumer_key = "ZKMBsV5ETf1erkMYdywTg"
              config.consumer_secret = "TsA0p3PWLcxc7CVmNBHJbhsOaeKs13BygsONgDQeM"
              config.access_token = "1835423847-psdbfLDsvjvBsApR3t0WvRI28C7QgStgeFabaHi"
              config.access_token_secret = "66aqNIqsFTlgDKRTdh0ZHrSKrplQKdjKMBodcsXzkGASU"
        end
    • Restart the server, if its already running

    • Make a request to localhost:3000

    • Placed a tweet in text area and click on tweet button

      alt tweet-article

    • It displays success response or else exception message

    • Tweeted message is like this

      alt tweet-posted

  • Facebook App verification

    • If you need to verify facebook application, follow these steps to get verified

    • Confirm your application is verified or not, with green circle in App Dashboard page, looks like

      alt fbapp-dashboard

    • Enable the verification of Facebook App using follow steps

      • Check App status

      alt fbapp-not-verified

      • Add your contact email to get verified

      alt verified-fb-app-contact

      • Now, Enable the app

      alt verified-fb-app

      • Finally App is verified

      alt fb-app-confirmed

facebook-twitter-posts-with-rails-example's People

Contributors

pmallesham avatar rdurgarao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

facebook-twitter-posts-with-rails-example's Issues

sync posts from Facebook and twitter to rails app

Hi, thanks for this great guide, I would like to ask if you have nay clues or resources on how to make the contrary, let me explain:

Let's say I have a model Post in my rails app, users can add posts, but how to publish in Facebook and also add a new post in the user account in my rails app?

I can't find any guides, or examples on how to achieve this. any ideas?

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.