jeudi 30 juillet 2015

How to share image on Facebook using fb_graph gem in rails

I want to share image on Facebook using fb_graph gem but i don't understand the exact procedure that how to implement in localhot.

I not use "localhost:3000" but i used "lvh.me:3000" because in my application i used subdomain concept so when user login then url is change to "xyz.lvh.me:3000/"

in my controller :-

class FbShareController < ApplicationController

def auth
    @album = current_photographer.albums.friendly.find(params[:album_id])
    @album_photo = @album.album_photos.friendly.find(params[:photo_id])
    cookies["title"] = { :value => @album_photo.photo_title, :expires => 1.minute.from_now }
    cookies["url"] = { :value => @album_photo.photo.url(:original), :expires => 1.minute.from_now }
    @client = client
    redirect_to @client.authorization_uri(
      :scope => [ :user_photos, :publish_pages, :user_posts]
    )
  end

  def callback
    # @album = current_photographer.albums.friendly.find(params[:album_id])
    @client = client
    @client.authorization_code = params[:code]
    access_token = @client.access_token! :client_auth_body # => Rack::OAuth2::AccessToken

    me = FbGraph::User.me(access_token)

    title = cookies["title"]
    url = cookies["url"]

    me.feed!(
        :message =>  title,
        :link => url,
        :name => title,
        :picture => url
    )

    redirect_to album_path(@album)
  end

  private

  def client
    if Rails.env.development?
      key = "xxxxxxxxxxxxxxx"
      secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    else
      key = "xxxxxxxxxxxxxxx"
      secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    end
    fb_auth = FbGraph::Auth.new(key, secret)
    client = fb_auth.client
    client.redirect_uri = "http://#{request.host_with_port}/fb_share/callback"
    client
  end
end

I create app on my Facebook named lvh.me:3000 and Facebook provide me two IDs like User id and App id and also provide App secret key.

So which combination i have to use in my controller ?

fb_auth = FbGraph::Auth.new(User_id, secret) OR

fb_auth = FbGraph::Auth.new(App_id, secret)

However, i already used both combination but i get following error:-

{
   "error": {
      "message": "Error validating application. Cannot get application info due to a system error.",
      "type": "OAuthException",
      "code": 101
   }
}

Whats wrong with my code i don't know.

I don't know that how to set configuration in facebook for this.

I need step by step solution if any one have because i am totally new in rails.

Any one have proper solution or steps because i already spent much more time behind this but i have no idea what to do.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire