Can't access ViewBag data in Facebook App - facebook

I have a Facebook app that is giving me fits. I basically have an app that once the user has liked the app, they can continue through the app. Using the Facebook Javascript API, it will see if the user has logged in, and if not allow them to log in. I pass rather or not they "like" the page from the controller, as well as their language (English or Spanish) to the view via the ViewBag object.
This works both locally and from the staging server; however I have to disable the like check in the view, but the language and like is passed in the viewbag.
I placed language in the view like
<p>The user's language is #ViewBag.lang</p>
and
<p>They like the page? #ViewBag.likes
Like I said, both locally and at the staging server it works; but nothing gets passed through to the Facebook app. I have a conditional check to see if the user likes the page in javascript like:
var likes = #ViewBag.likes
In Facebook, using the console I see that the data isn't passed as well as the before mentioned debugging I tried doing.
I am very new to MVC; is there something I need to enable to make it work through Facebook?
Thanks!

Well the issue is that the Facebook canvas sends an initial POST, this was hitting my POST instead of the GET which was setting these values. See here:
Does Facebook open a canvas app with a Post request? It's causing havoc with my MVC actions
Thanks!

Related

Facebook Like button inside Delphi program

I would like to add a Facebook Like button inside my software.
I know how to programmatically do the actual liking process,
but how do I check and see if a person actually has liked my page?
Edit: I'm guessing that my software also has got to communicate with an actual Facebook App
that I have to develop, and not Only with a regular facebook html source?
Have you considered dropping a TWebControl on your Delphi form, assigning a simple HTML document string to it that contains the markup needed for a Facebook like button and making it just large enough to display your facebook like button content?
The like count and user liked state would be handled automatically by the javascript dynamically loaded into the html page. The javascript written by Facebook.
Seems like a full-functionality, least-effort approach worth considering.
I don't have Facebook to test this, but try to use what is described in this blog post; try to use the following HTTP GET request and in the response (after you parse it), find the user you're looking for:
https://graph.facebook.com/me/likes/PAGE_ID&access_token=ACCESS_TOKEN
Where PAGE_ID should be your page ID and ACCESS_TOKEN your access token.
For a simple GET request you can use e.g. this code.

Facebook opengraph. Meta tags, localhost, custom actions, and testing

This is a series of questions about implementation with the Facebook open graph.
So, I make a query to the api as such: $this->facebook->api('me/namespace:action', 'post', $args );
My first query is where do posts appear? For example if I make a call with the parameters as follows:
$args = array(
//'message' => 'I just posted a pub review at PubJudge.com',
'website'=>'www.pubjudge.com',
'user'=>current_url(),
'link' => 'http://www.pubjudge.com/',
'caption' => 'Independent, social, pub reviews.'
);
The post appears on my timeline under 'Activity'
As far as I understand, if a user explicitly shares an action it will appear on the timeline. Is this correct?
Is "fb:explicitly_shared" a parameter that should be passed in the $args array?
Next comes Facebooks custom meta data. In this case my obect is a user, and I pass their URL through the $args array. Facebook then scrapes this array to get data from the custom meta tags on this link. Is this correct?
Does this url have to be the url from which the action is initiated? Can I not have a page on my website which simply generates custom meta tags based on a $_GET variable - this way I can keep all this complicated Facebook stuff independent of the main site.
Does anyone know why Facebook gets data for open graph actions like this anyway?
Finally because Facebook scrapes data like this I cannot test this functionality on my localhost. (I could do but it'd be very complex) What I have opted for instead is a testing.domain.com address to test my website and this functionality whilst having domain.com as my main live stable site.
I have also created two apps.. my main app, and a testing app because if I use my main app I have to change the site url to testing.domain.com which then breaks my stable site. Is this a suitable approach?
As far as I understand, if a user explicitly shares an action it will
appear on the timeline. Is this correct?
Yes, that's correct. You can find more information at the Facebook docs for explicit sharing. To be noted from the docs, you'll have to mark your action as able to explicitly share on your app dashboard and Facebook will have to approve it.
Is "fb:explicitly_shared" a parameter that should be passed in the
$args array?
Yes.
Next comes Facebooks custom meta data. In this case my obect is a
user, and I pass their URL through the $args array. Facebook then
scrapes this array to get data from the custom meta tags on this link.
Is this correct?
Yes, Facebook scrapes the URL you pass so they can get data from the OG tags at that link.
Does this url have to be the url from which the action is initiated?
Can I not have a page on my website which simply generates custom meta
tags based on a $_GET variable - this way I can keep all this
complicated Facebook stuff independent of the main site.
No, it does not have to be the url from which the action is generated. You can have a separate page on your website, and in fact you can even use javascript to redirect users who land on that page to a more appropriate page.
Does anyone know why Facebook gets data for open graph actions like
this anyway?
That's probably a deeper question than you realize. In short, they are trying to create a semantic web. The long answer would be an essay.
Finally because Facebook scrapes data like this I cannot test this
functionality on my localhost.
You can use reverse proxy which works in most cases (I've seen problems with photos if you are using a port for the reverse proxy). Not very complicated. Something like the following works well:
ssh -nN -f -R externalhost.com:49080:localhost:80 your#credentials.biz
I have also created two apps.. my main app, and a testing app because
if I use my main app I have to change the site url to
testing.domain.com which then breaks my stable site. Is this a
suitable approach?
Yes, that's normal. We typically have a development app (localhost), a staging app (external host for testing), and a production app.

Can an in-app object only be posted through an opengraph action?

I have a page inside a facebook application that is an opengraph object, when I post a custom action on this object to facebook from my app it is posted right, but when I use a facebook social plugin such as like and like that object it is not posted as that object but instead the object of my facebook application is posted on the timeline... so my question here is Can an in-app object only be posted through an opengraph action?
Nope, new OpenGraph object will be created or data will be updated for existing one once Facebook linter crawl your page to get data this will happen in several cases:
OpenGraph action referencing object published
Like button clicked for specific URL
Link to your page shared on Facebook (in direct way or via any dialogs using link, etc).
Your Like button is probably linked not to your real application URL but to URL within Facebook resulting in different Pages parsed by Facebook on Like Button click and OpenGraph action publishing.
I assume that your like button pointing to Application Tab Canvas or Application Page since links to regular application's canvas parsed correctly by Facebook.
Update:
Seems like the issue with OpenGraph tags is related to the fact that your application returning 404 (Not Found status code) for URL you provided and only returns data for HTTP (but not HTTPS) requests. If error code is returned the cached data is preserved and will not be updated until correct status code returned.
Update 2:
As you've provided real URL it's became clear that you get details for your application instead of actual page because of redirect for all unauthorized users, which lead to inability to rich the real OpenGraph data by Facebook linter.
BTW, You should be aware that every OpenGraph object MUST have publicly accessible URL.

How to redirect the user to the log in page in a Facebook App

I am hosting a Facebook app on Google app engine, I need to make sure the user is logged into facebook before anything.
What I can currently do is display facebook's log in button using fbxml, but I prefer the user would be redirected to Facebook's log in page if he wasn't logged in, then back to my app's main page, this way I can make sure that the user is logged in before doing anything.
I am new to Facebook apps, I read here that I can redirect the user to
https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL
in order to authenticate him. But using GAE's self.redirect(url) doesn't work, the page stays the same. I was hoping I could do something like this in my handler:
if u'signed_request' in self.request.POST:
facebook.load_signed_request(self.request.get('signed_request'))
if not facebook.user_id:
self.redirect("https://www.facebook.com/dialog/oauth?"+
"client_id={0}&redirect_uri={1}"
.format(FACEBOOK_APP_ID, EXTERNAL_HREF))
return
but as i said earlier this doesn't work.
I'm assuming by 'Facebook App' you mean a Canvas App - so something that will live at https://apps.facebook.com/YOUR_NAMESPACE from a user perspective?
If so, you'll need to add the redirect via Javascript using window.top, as your app is loaded in an iframe. See https://developers.facebook.com/docs/appsonfacebook/tutorial/ and search for 'top', then view the example toward the end of the page.
There are also good examples on the Php Sdk that facebook provides. I like the with_js_sdk.php example. It runs seamlessly and is good to follow.
https://github.com/facebook/php-sdk
http://developers.facebook.com/docs/reference/php/

Facebook Like Button Keeps Resetting

I'm trying to add a facebook "like" button to my page, and it is appearing just fine. My problem is that once it is clicked by a user, it says "Like NUMBER" then resets as if the request was canceled or something.
The weird thing is that if I try to like a page using my facebook account it works, but I tried letting a friend like some content and this behavior appeared.
I created a facebook app, specified the site url with a trailing slash, and site domain. I used the app ID with FB.init, and with the open graph tags. I can't figure what I'm doing wrong here. Any help would be appreciated.
Sample page can be found at: http://wiseolive.com/en/doctors/3881-khalid-jamal-salaymeh
First, for the count to work correctly, Facebook needs to have access to the page (in other words a public page). Check your URL in the linter tool (https://developers.facebook.com/tools/debug) to ensure it can been seen by Facebook.
Secondly, ensure your og: tags are correctly setup. Once again, you can use the linter tool to do that.
Facebook like buttons will show this behavior until they have been scraped by Facebook. That scraping (facebook like docs) will occur every 24 hours, when an admin clicks the like button or the url is put in the url debugger/linter. From what I can tell doing that will solve the problem for that specific link, but not any others you may have on your site.
I don't know of a generic solution for dynamically generated pages unfortunately.