This course is archived!
The concepts of OAuth2 we're teaching in this tutorial are still valid, but the course project code is ancient.
07.
Facebook: Using the API, Logging in and Failure
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
16 Comments
Is there an updated version of this course or code? It uses Symfony 2.4, though it seems that many of the concepts of authorization are the same in Symfony 4. Still, if there's something newer...
Hey Michael,
Yes, you're right, this course is kinda old already but OAuth concepts we covered in it are still relevant. Unfortunately, we don't have any updates yet.
Cheers!
I'm trying to use the new php sdk. 3.2.3 version tell me that is deprecated. But I missed something. I don't know what. I ca't obtain access token in shareProgressOnFacebook function. I'm using the same code for all two functions from FacebookOauthController that requires $accessToken. I haven't any problem for receiveAuthorizationCode function but in shareProgressOnFacebook if i try to var_dump accessToken it's null. I used https://developers.facebook.com/docs/php/FacebookRequest/5.0.0 code to replicate the older code but with no luck. I have no idea how to correct things.
My code is like so:
and function handleFailedTokenRequestORGetAccessToken is like so:
Hi Diaconescu!
Sorry for my slow reply! Ok, if I understand things correctly, you can get the access token from inside
receiveAuthorizationCode, correct? But you cannot get it from inside ofshareProgressOnFacebook.If that's correct, here's the reason: you can only fetch the access token from inside of
receiveAuthorizationCode. Specifically, you can only fetch the access token right after Facebook redirects back to your site. That's because when this happens, Facebook adds a ?code= to your URL. When you callgetAccessToken(), my guess is that this function reads the ?code= parameter and uses it to fetch the access token. That's native to how OAuth works: you're exchanging the authorization code (?code=) for an access token. You cannot fetch an access token at any other time.But, that's ok! When you fetch the access token in
receiveAuthorizationCode, you just need to store it in the database or on the user's session so that you can use it later. In other words, save the access token so that you can use it inshareProgressOnFacebook. Just know that the access tokens are not permanent. If using the access token fails, you'll need to send your user back through the OAuth flow to get a fresh access token.Cheers!
Got stuck with the following:
$json = $facebook->api('/me');
var_dump($json);die();
On clicking 'Login with FB' returns only
array (size=2)
'name' => string '{My Name}' (length=14)
'id' => string '#' (length=17)
No e-mail or other data.
Is it a problem connected to the old SDK or smth else?
Hi Mihail!
Hmm, I'm not sure. I would verify that you're sending the right scopes when you redirect to FB. It's possible you're not asking for any scopes, so you're *only* getting back the name + id. Btw, is the id literally '#'? That definitely seems weird.
It's possible that the old SDK finally doesn't work with the Facebook graph API, but I haven't heard of any other issues (yet).
Cheers!
Hi Ryan!
Thank you for your reply!
Id is fine, I just used dump symbol.
As far as I understand the scope is set right, according to the tutorial in FacebookOAuthController::redirectToAuthorization()
$url = $facebook->getLoginUrl(array(
'redirect_uri' => $redirectUrl,
'scope' => array('email', 'publish_actions')
));
Hi Mihail!
Hmm, yes, I just tried it again with the real code that's included in the code download and it *does* work as expected. When I dump the $json, I'm returned an array with id, email, first_name, gender, name and about 5 other fields. It's possible that when you originally authorized your Facebook application for your account, you only authorized it for 1 scope (but not all the scopes we were asking for). You can remove authorization for your app (https://www.facebook.com/se... and try it again - that might fix it.
Cheers!
I have the same issue as Mihail.
Only info I get from FaceBook is name & id.
I've tried to re-authorize Facebook app with no luck.
Also when authorizing Facebook app, I get a message:
"Submit for Login Review
Some of the permissions below have not been approved for use by Facebook.
Submit for review now or learn more."
which wasn't shown in tutorial.
Screenshot: http://cl.ly/1i0t1K2r3a0o
Hey Kuba!
Ah, fascinating! Your screenshot might lead to the solution! It seems that Facebook now requires your application to be "reviewed" by them in order to ask for *certain* scopes. If you ask for the "email" or "public_profile" scope, then your app "Top Cluck" does not need to be reviewed. But if you ask for publish_actions (as we do), then your app *does* need to be reviewed. I'll see if I can get them to review our very awesome TopCluck app :).
So, if you do *not* ask for the publish_actions scope, do you then get back more information?
Cheers!
Ah, also see this issue: https://github.com/knpunive... - it could be the real solution :)
For anyone else who winds up here, simply adding the fields wasn't enough for me as I still couldn't get access to "email". I'd get first_name, last_name, and ID. To get email, I had to make a new "test user" in Facebook's app page. It is my understanding you may not retrieve all information from real users prior to app review, but test users will work regardless.
Hmm what Facebook PHP package are you using? I'm using "facebook/graph-sdk": "^5.4", and I haven't seen these methods/services/exceptions
Hey Gytis,
For the client application in this screencast we use "facebook/php-sdk" v3.2.3.
Cheers!
I see. Looks like your package is older, now abandoned version.
Yeah, it's crazy how fast the web evolves :)
"Houston: no signs of life"
Start the conversation!