简体   繁体   中英

I can't get PHP Facebook API to work

So I want to implement Facebook login on my site. I have dowloaded the SDK, created an app and pasted the app id and secret in.

The problem I am getting is that, while the login flow appears to work and I am getting values returned ok from

facebook->getUser()

and

facebook->getAccessToken()

Calls to

$user_profile = $facebook->api('/me','GET')

throw an exception saying I have no access token.

Since this is unmodified example code straight from the SDK I'm kind of at a loss - if the coding example given doesn't work how am I supposed to figure anything out? :)

Same issue with the code on this page:

https://developers.facebook.com/docs/php/howto/profilewithgraphapi/

Basically that does the same thing but handles the exception so I go round and round in a "login to facebook" loop.

I can see the app added to the list in my facebook profile OK when I log in so that side appears to be working. It seems that either the SDK is broken or the access token I'm getting back is for some reason not valid.

Any help or pointer as to what I've missed would be much appreciated!

Thanks

Justin

Try this

$config['cookie'] = true;

and relogin.

This is from the PHP code I use to login.

Here is the complete flow of the code hope this helps.

Say the page for the FB connect is login.php and I will have the following PHP code on the top of the page

require_once 'src/facebook.php'; //include the facebook php sdk
 $facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_APP_SECRET,
    'cookie' => true,
    'domain'=> FACEBOOK_CONNECT_DOMAIN 
  ));

 $user = $facebook->getUser();
 if($user){
   // lets set the access token before making a api call.
   $new_access_token = $facebook->getAccessToken();
   $facebook->setAccessToken($new_access_token);
   $user_profile = $facebook->api('/me','GET') 
   ......
   ......
 }else{
  // display the fb login  
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM