0
votes

So I'm re-writing our old Facebook app (from 2011), bring it up to snuff. It's a straight Canvas app - I just want it to run in the iFrame, it doesn't need any special permissions. I am trying to work from the sample in the PHP SDK docs, and I get no FacebookSession. Here's what I'm working with:

// initialize the FacebookSession with our app and secret
FacebookSession::setDefaultApplication($this->fbapp_settings['appId'], $this->fbapp_settings['secret']); 


// create a canvas login helper to deal with login stuff
$this->loginHelper = new FacebookCanvasLoginHelper($this->fbapp_settings['appId'], $this->fbapp_settings['secret']);

try
{
    $this->facebookSession = $this->loginHelper->getSession();
    if (empty($this->facebookSession) )
    {
        throw new Exception("Error, no facebook session created");
    }

    // this is where my code will do something cool

} 
catch (FacebookRequestException $ex) 
{
    throw new Exception("FacebookException: " . $ex->getMessage() );
}
catch ( Exception $ex)
{
    throw new Exception("Elite Exception: " . $ex->getMessage() );
}

I tried with with "Client OAuth Login" both on and off. Looking in the code for FacebookCanvasLoginHelper->getSession, the problem is that the signedRequest that I get has no OAuthData. Weirdly, it seemed to be working fine early yesterday.

Any thoughts?

Thanks, Andy

Addendum: Per the previous comment, I re-added the login dialog. So my flow is: a) Facebook calls my app with a signed_request, which I use to create a FacebookSession, which doesn't have OAuth credentials, so I invoke the Login dialog (created via the FacebookRedirectLoginHelper->getLoginUrl function). The login dialog comes up. I press ok, and Facebook invokes my app again, with a code this time instead of a signed_request. I try to create a FacebookSession - first I need an AccesToken, so I invoke AccessToken::getAccessTokenFromCode, which throws an exception:

'Facebook\FacebookAuthorizationException' with message 'Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request'

Once my app is given permission (through the login dialog) by the user, if I want to get the UID, is this the correct way to do it (i.e., using the "code" to create an AccessToken and use that to create a FacebookSession)? There is no place in the flow that I can see where i can input the redirect_uri - GetAccessTokenFromCode explicitly sets it to an empty string, I'm guessing that is the problem here.

thanks for your help, andy

1
Did the user authorize the app before already? If you want this to work for new users, then you have to call the login dialog. - CBroe
Thanks, I was doing that earlier yesterday, but in reading the Canvas docs, I got the impression it wasn't necessary if I didn't need extra permissions. Sigh. I'll work that back in. - Andy Wallace
FacebookRedirectLoginHelper will generate the login URL for you, and can process and validate the data from Facebook, returning a FacebookSession on success. So if you should get the session using $helper = new FacebookRedirectLoginHelper($redirect_url); $session = $helper->getSessionFromRedirect(); - Nithya Sivakumar
But if I'm making a Canvas app, shouldn't I be using the FacebookCanvasLoginHelper, which only has the "getSession" function? This part is working, I'm getting a session at that point. But then, when FB invokes my app after getting the user to give permission, I get a code instead of a signed request. So I try AccessToken::getAccessTokenFromCode which internally sets "redirect_uri" to a blank screen, then sends that off to Graph, which rejects it with the "Error validating verification code" error. - Andy Wallace
Well, that got me past that hurdle - explicitly calling AccessToken::requestAccessToken instead of getAccessTokenFromCode gets me an access token. One tiny step forward. But the session created via this code doesn't seem to have a UID in it. - Andy Wallace

1 Answers

0
votes

Well, I decided to take a leap of faith, and now things have progressed. I:

a) set up a FacebookRedirectLoginHelper at the top of my app

b) get the loginUrl from that, using a pointer to my app (mydomain.com/myapp), and present that for authorization

c) user clicks on that link, gets the Facebook OAuth screen, if they say ok, then Facebook goes back into my app via the redirectUrl embedded in the loginUrl. Using the FacebookRedirectLoginHelper, I get the Facebook Session, and I'm good, I can make a FacebookRequest to get the userProfile and the UID.

There is still a problem, going to open a new ticket for that one.

thanks to the commenters.