0

I believe I have read almost all if not all the related posts, but I still can't find my answer.

On client side I get the user to login and send user id and access token to server. Then server use facebook->getUser() to check if the sent userid really belong to him, if so I add him to my database and register him. similar to this question

Facebook Login: How to combine JavaScript with PHP SDK?

except facebook->getUser() keep returning 0. I have look through A LOT of questions related to that too, but I just cant find my answer. No problem with client side though, user can log in and give authority, it generate the correct userid and a token.

I am currently using localhost to test everything.

php:

<?php
require 'facebook/src/facebook.php';

$uid = $_POST['userid'];
$token = $_POST['accessToken'];


$facebook = new Facebook(array(
  'appId'  => 'XXXXXXXXXXXXXXXXX',
  'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));

// Get User ID
$user = $facebook->getUser();

if($user==$uid){
//codes here

My question is similar to the following posts but it didnt get any answer Facebook API: Login using JavaScript SDK then checking login state with PHP

additional info: the reason why I want to use PHP sdk too is because, I dont want people to use someone else's fb userID to send request to my server 'pretending' to be another person

Community
  • 1
  • 1
user308553
  • 1,238
  • 4
  • 18
  • 32

1 Answers1

1

You receive the FB Token from your FB Javascript SDK. Are you setting the FB Token in PHP first with the one you received from Javascript?

$facebook->setAccessToken($new_access_token);

before calling?

$facebook->getUser()

zer02
  • 3,963
  • 4
  • 31
  • 66
  • Actually I'm doing something similar and have set cookies and status to true in the JS-SDK. I *do* not need to call setAccessToken but get the correct user is when I call $facebook->getUser() in PHP-SDK! I've tried logging out the client/browser facebook and generating a AJAX call and I get the a error getting the user (OAuthExpeption with message that token is invalid) or getUser returns zero if you refresh the page on browser and do the ajax call. – Nilesh Kale Feb 18 '14 at 04:01