Ruby on Rails view Facebook Event without user login - ruby-on-rails

I'm putting together a website which displays Facebook events using Ruby on Rails and Koala.
I can display an event just fine if I do the following:
graph = Koala::Facebook::API.new(auth_token)
fb_event = graph.get_object(some_event_id)
But as soon as I change the first line to:
graph = Koala::Facebook::API.new(auth_token)
Ie, without the auth_token, I get an error--> type: OAuthException, code: 104, message: An access token is required to request this resource. [HTTP 400]
I don't want the user to have to be logged in in order to view the event. I know that I don't have to be logged into Facebook to view the event, so I'm sure there must be way to get the event without having to have the user be logged in. Any ideas?

Using WizKid's comment to get me there:
I went to https://developers.facebook.com/tools/access_token/
I then used my app's "User Token"

Related

Rails 5 - how to implement ajax flash message for devise

I am making a rails5 blog app with devise. In the comments controller, I check if the user exists using "before_action :authenticate_user!".
So, if the user is logged in then create comment method gets executed and the server response gets send back to the client via AJAX. However, if the user is not logged in then "before_action :authenticate_user!" check creates a 401 authentication error in the response ( can be seen in the browser log).
I want to just display the flash message just above the comment box or somewhere on the page so that user knows that he/she can not enter a comment without logged in.
There are many similar questions I have found but they seem very confusing and none of the solutions worked for me. Most of them talk about the changing the devise confg (e.g. config.http_authenticatable_on_xhr = false) or override the authenticate_user method etc.
Can someone just explain is it possible to show a flash message warning to the user to login to add comment (ajax call). I don't want automatic redirect for AJAX calls.
Create a flash method then catch the unauthorized error in jQuery and show your flash message :
#flash = (content, type) ->
content = "<div class='container'><div class=\"alert alert-#{type}\">#{content}</div></div>" if type?
$('#flash').clearQueue().hide().html(content).fadeIn('slow').delay(3000).fadeOut('slow')
$(document).ajaxError (e, xhr, settings) ->
flash("Unauthorized", "danger") if xhr.status == 401

Getting Facebook Page's Ratings

I am looking to pull a public Facebook page's ratings into a Ruby on Rails app.
Essentially what I'm looking to do is run a rake task as a cron job that looks for new reviews on one particular Facebook page (the page I need will not change) and pulls them into the app. I've explored Facebook's API and the Koala gem and can't get to the page data. I'm just not sure how I can get an access token outside a browser. In addition, if I were to get the access token, it doesn't seem I can get to the ratings data without being the owner of the page.
One route I took was to send a simple GET request to the page. Something like https://graph.facebook.com/{page-id} works fine, but https://graph.facebook.com/{page-id}/ratings throws the following:
{
"error": {
"message": "(#210) Subject must be a page.",
"type": "OAuthException",
"code": 210
}
}
What is the simplest way to get to this data?
As mentioned in the question's comments, I needed to be authenticated as a Facebook user with admin access to the Facebook Page of interest because I need to get a token from that.
After getting access to the page, I accomplished the API call via the Koala gem. I initialized Koala with the app:
config/initializers/facebook.rb
$facebook = Koala::Facebook::OAuth.new(
"my_facebook_app_id",
"my_facebook_secret",
"my_facebook_redirect_url"
)
Since I only needed to get the token for one user in one scenario, I did so via the admin side of my app, in my facebook_controller.
In this controller action, I look for the code parameter in the route. If we don't find it, we send to the authorization url, otherwise we process and store the Facebook user token in the User model in a pre-determined and predictable record.
app/controllers/admin/facebook_controller.rb
def auth
if params[:code].present?
code = params[:code]
token = $facebook.get_access_token_info(code)
User.find_by_email('admin#example.com').update(
:fb_access_token => token['access_token'],
:fb_token_expires => token['expires'].to_i.seconds.from_now
)
redirect_to admin_dashboard_path
else
redirect_to $facebook.url_for_oauth_code(:permissions => "manage_pages")
end
end
Once I have an active token, I can acquire the page token like so:
user = User.find_by_email('admin#example.com')
user_graph = Koala::Facebook::GraphAPI.new(user.fb_access_token)
accounts = user_graph.get_connections('me', 'accounts')
page_token = accounts[0]['access_token']
page_graph = Koala::Facebook::API.new(page_token)
ratings = page_graph.get_connection('me', 'ratings')
At this point, I have the collection of ratings objects stored in the ratings variable. I can do whatever I need with these ratings at this point.
Note: In this particular case, I new the Facebook Page was the first account in the array of user accounts. This took some trial and error, but can be accomplished by iterating over the user accounts array (in this case, stored in accounts) if you can't predict the index of the desired account.
It seems to be a bug of Facebook Graph API. I have exactly the same problem.
I can get lists of all other objects but I do get an error when requesting /{page-id}/ratings. The JSON payload returned by Facebook Graph API is the following:
{
"error": {
"message": "(#210) Subject must be a page.",
"type": "OAuthException",
"code": 210
}
}

how to get screen name of twitter using codebird js

1.2.GA and Android Emulator to run my Apps. AM using codebird to authorize my app in twitter and I got access token and using it how to get screen name or userid to fetch tweets of the user?
When the user returns from the authentication screen, you need to trade
the obtained request token for an access token, using the OAuth verifier.
As discussed in the README section ‘Usage example,’ you use a call to
oauth/access_token to do that.
The API reply to this method call tells you details about the user that just logged in.
These details contain the user ID and the screen name.
Take a look at the returned data as follows:
{
oauth_token: "14648265-rPn8EJwfB**********************",
oauth_token_secret: "agvf3L3**************************",
user_id: 14648265,
screen_name: "myx",
httpstatus: 200
}
If you need to get more details, such as the user’s latest tweet,
you should fetch the complete User Entity. The simplest way to get the
user entity of the currently authenticated user is to use the
account/verify_credentials API method. In Codebird, it works like this:
cb.__call(
"account_verifyCredentials",
{},
function (reply) {
console.log(reply);
}
);
I suggest to cache the User Entity after obtaining it, as the
account/verify_credentials method is rate-limited by 15 calls per 15 minutes.

Koala - Create tab in facebook (rails)

I want to create tabs on FB.
I have this code:
#graph = Koala::Facebook::API.new(Client.find(session[:id_client]).oauth_token)
#graph.put_connections("4154829881847172/tabs","POST", {:access_token => "AAABwdtYjsyoBAMcg558a4FYnZBkZBUiPKmcWWssssWoNZC2yjqE43ghoR9uTiFIhT3ErkQYx45RrrPeOD0ZCvFgnmRIUh9lqRUw5KIlWwxtRW3GvbIEUWp0yB2", :app_id => '1236553701115690'})
But I have a problem
-> OAuthException: (#210) Subject must be a page.
Is 4154829881847172 the correct ID for the page you're trying to add the tab to?
And are you definitely using the page's access token? (as opposed to a user access token for the page admin)
Either of those being incorrect means you're passing a something-other-than-a-page's ID in place of the Page ID
This did not work for me as well. And I think the reason is http://developers.facebook.com/bugs/194192344040832/. Running this on over Graph API Explorer gives the same result.
But I did using the following way
#graph = Koala::Facebook::GraphAPI.new("user_access_token")
#newgraph=Koala::Facebook::GraphAPI.new("page_access_token")
#newgraph.put_connections("me","tabs", {:app_id => 'your_app_id'})
You basically make a graph object with the page access token and then run the put_connections method.

Using fb_graph to post to Facebook fan page from Rails 3

I hear that fb_graph is the way to go and I already have my app registered with Facebook but I don't know how to get the access token to post things. I have my app ID and secret but I need to get that access token. All I'm trying to do is post to a Facebook fan page (as the page).
How do I get the access token?
Get the user:
user = FbGraph::User.me(access_token)
user.fetch
To see the users accounts details:
user.accounts
Select the Facebook page that you want to post to:
account = user.accounts.select {|account| account if account.name == "*Your Page Name*"}.first
(account.access_token => the pages access token)
(account.identifier => page id)
Create new page instance:
page = FbGraph::Page.new(account.identifier)
Post to the page:
note = page.note!(:access_token => account.access_token, :subject => "Hello World", :message => "hey, this is a test from rails")
The step-by-step procedure for access token with Oauth 2.0 is found here: http://developers.facebook.com/docs/authentication/. If you require a sample code in Ruby, create a new app and under "Hosting URL", click on "get one" and select Ruby programming language when prompted. It also contains code to pull from Graph API.
I highly highly recommend the new gem Koala for working with facebook.
If you do a call to https://graph.facebook.com/me/accounts
and pass in your access_token it will return data listing all the pages you have access to and the access_tokens needed to post to those walls.#
You get your access token by logging in and the access token is passed back by the login process.

Resources