Difficulty getting all pages a user likes - ruby-on-rails

I use this code, it get all pages the user likes:
#user = session[:graph].get_object('me')
like = session[:graph].get_connections("me", "likes")
if !(like.to_s.include?('appid'))
redirect '/youneedlike'
end
It's works for me and my mates, but some users have error: they are always redirected, even though they like our page.
What's wrong?

Does your app request the user_likes permission?
Alternatively, if your app is a canvas app, you can use the signed_request parameter to do the same thing:
# pseudocode
signed_request = decode signed_request()
if signed_request['page']['liked']:
# user liked page, do something cool
else:
# user doesn't like page. redirect somewhere to tell them why they should

Related

Indicating intent with omniauth facebook

I'm using the omniauth-facebook gem and would like to pass Facebook a string indicating whether the user is trying to join or login, so that I can use the information when they return to my site.
It would be great if I could do something like this:
= link_to 'Join with facebook', '/auth/facebook?intent=join'
= link_to 'Login with facebook', '/auth/facebook?intent=login'
In case anyone wants to know why I want to do this, here's what I plan to do with the information:
# pseudo code
- if authentication fails
- if user was trying to join using facebook
= return them to the signup url
- elsif user was trying to login using facebook
= return them to the login url
So is it possible to signal my intent to facebook, and have them return that intent with their response so that I can use it if I need to?
I'm not using Devise, and would prefer to avoid having to set session variables to do this, Facebook must surely have a solution for this need...
Looks like this is possible and my code does work. When I inspect the response from facebook, I see the following:
>> request.env['omniauth.auth']['intent']
=> nil
# no dice
>> params
=> returns lots of stuff, but not the thing I want
# worth a try
>> request.env['omniauth.params']
=> {"intent"=>"login"}
# bingo
So the params hash that rails puts together does not contain the desired info, but the response does contain a bunch of unsigned details, and this is where the information I want can be found.
Worth noting you can also call:
>> request.env['omniauth.origin']
=> "http://localhost:3000/join"
Pretty cool.

Capture The Original URL After A Redirect?

I am using Rails 3 and Devise for authentication.
I want to track whenever a user enters my site from another domain or by typing in the URL. Assume the following page on my site:
http://www.mysite.com/somepage
If a visitor requests this URL by clicking a link on another site or types it in to his browser, I want to put the URL into a cookie for later use. If /somepage does not require login it works fine. In a before_filter I just check to see if the referrer is not from mysite.com.
However, when /somepage requires a login, Devise makes a redirect, which results in a second request to my login page. The original referrer is carried forward to the new request. So my code thinks it's the original requested URL and overwrites the cookie. Wrong.
I'm probably just having a mental block, but I can't see how to determine that the page that is redirected to is not original page requested.
You could probably get some ideas from this article.
It seems like you should filter out anything that includes the user path, since you have no problem getting the referrer if the page doesn't require login.
So for example you could use a regex to filter out users/sign_in, users/sign_out, etc.
request.referrer = request.fullpath unless request.fullpath =~ /\/users/
or
request.env['HTTP_REFERER'] = request.fullpath unless request.fullpath =~ /\/users/
You would need to put this in some kind of before_filter above the authenticate before_filter so it is called first.

Fetching FB pages of a user using Koala

I have an FB account and i have created a fan page for my club and few other pages as well. I have an app in Ruby on Rails. I want to publish some feed on my club fan page. How can i do this?
I have been using Koala Gem and able to successfully post to my wall but not on to the page.
I want to access the list of all the fan pages associated with my account instead of giving the name of specific page.
here is my simple method which i am using to communicate to FB Graph API.
def facebook
#facebook ||= Koala::Facebook::GraphAPI.new(oauth_token)
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
Answer submitted by Sumit can be an approach but after searching around some more forums, finally i got an elegant way to do it.
#user_graph = Koala::Facebook::API.new(user_access_token)
pages = #user_graph.get_connections('me', 'accounts')
# get access token for first page
first_page_token = pages.first['access_token']
# or: retrieve access_token for a given page_id
page_token = #user_graph.get_page_access_token(page_id)
Passing on the "accounts" parameter to get_connection worked elegantly for me.
Here is the reference to API.
And one last thing, never forget to add the "manage_pages" permission in your permissions list.

Devise: how to store a particular location, not necessarily the last location?

I'm wondering if I'm asking too much of Devise. I'd like to allow a visitor to fill out a form and then but then be required to sign up when they press the "submit" button. That's the easy part. My problem is that once the user is redirected to the sign up form and they fill it out, I can't get them redirected back to the original form so that that form can be submitted (but now with the user ID).
I've played around with the "after_sign_in_path_for(resource)" method in the application controller, but my problem is that after signup, the request.referer isn't the original form, it's the signup form. I thought about using a before_filter :store_location callback, but then I realized that it'll store the most recent location, which is the url for the signup form.
How do I set up devise that original form as the correct location to render after sign up?
P.S. This question comes from a related one, located here.
Try this make 2 method in ApplicationController which saves
the current url to the session, and another that redirects to the stored
url (which should have a default in case it can't find it). Then, in
pages which i want to bookmark for returning to (you might not want
someone to get sent back to some pages), then save the url.
EG in application.rb
def store_location
session['saved_location'] = request.request_uri
end
def redirect_to_back_or_default(default)
if session['saved_location'].nil?
redirect_to default
else
redirect_to session['return-to']
session['saved_location'] = nil
end
end
Then, in a page that you want the user to be able to go back to:
Then, just call
redirect_to_back_or_default('enter your chosen default url here, depending on context')
in after_sign_in_path_for(resource_or_scope) to send someone back.

rails not picking session data

I am implementing a payment gateway in my app.
Its like this:
The user fills the form with necessary details, along with a field containing return_url(say http://myapp.com/ebs_payment/ebs_response?dr={somedata}) and submit the form to a secure payment site. After the transaction is complete, the secure site puts some encrypted data into my param {dr} and the user is redirected back to the return url. The problem here is, when the user returns to the app with the return_url, the application fails to pick up the session data and returns a nil value.
Before submitting the form, I put the object #fdPaymentDets in to session.
Here is my controller:
class EbsPaymentController < ApplicationController
#before_filter :login_required,:check_user_authenticate
#access_control [:ebs_response] => ('user')
def ebs_response
#fdPaymentDets = session["fd_payment_details"]
#deal = Deal.find(#fdPaymentDets.deal_id)
#categories = find_all_categories
end
private
def find_all_categories
#sp = SitePreference.find(:first)
Category.find(:all,:limit => #sp.categories_display_limit,:order => 'position')
end
end
When the user is redirected to the return url (http://myapp.com/ebs_payment/ebs_response?dr={encrypted_data}) from the secure site, rails is not picking the #fdPaymentDets object from session and making it nil thus resulting in an error when accessing data from the nil object.
The surprising thing is that, when I put the same return_url in my browser by hand, the session data is neatly picked and everything goes well.
Am missing any thing here? What could be the obvious reason?
Any help appreciated.
Cookies and redirects are messy and bug prone (from a browser's implementation perspective).
Take a look at
Safari doesn't set Cookie but IE / FF does
Suggestion would be to change the implementation to set the session first in the show action, and then update the value before the redirect

Resources