I'm new with phpsdk v4, and I'm trying to get access token for my pages.
I tried with this:
<?
include getenv('social');
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookSDKException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookResponse;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
$access_token = $_SESSION['facebooklogged'];
$session = new FacebookSession($access_token);
And after:
$page_access_token = (new FacebookRequest( $session, 'GET', '/320330478029510?fields=access_token', array( 'fields' => 'access_token' ) ))->execute()->getGraphObject()->asArray();
print_r($page_access_token);
And:
$request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms');
$pageList = $request->execute()->getGraphObject()->asArray();
print_r($pageList);
Both return or id of page, or a empty array();
Array ( [id] => 320330478029510 )
Array ( )
I created $session from login page, with
$logare = new FacebookRedirectLoginHelper('https://mysite/facebook/login.php');
try{$session = $logare->getSessionFromRedirect();}catch(FacebookSDKException $e) {$session = null;}
if($session)$_SESSION['facebooklogged'] = $session->getToken();
What is wrong?
I tried with another account, another admin of page, and work with that...but with my account, still nothing ... I will delete this app from used apps, and i will try again.
Related
I have created 2 facebook apps, hosted in the same external domain. I have installed these two apps in a facebook page of mine and in both apps i am running the following php code at the beginning:
session_start();
// added in v4.0.0
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// start session
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','yyy' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/my_namespace/' );
try
{
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex )
{
// When Facebook returns an error
}
catch( Exception $ex )
{
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) )
{
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
}
else
{
// show login url
echo 'Login';
}
When I run each application, the user identity that is shownn is equal for all properties except for user id:
One app 1, i see:
Facebook\GraphObject Object
(
[backingData:protected] => Array
(
[id] => 1400416826952138
[email] => my_name#gmail.com
[first_name] => Name
[gender] => female
Etc
On app 2 I see:
Facebook\GraphObject Object
(
[backingData:protected] => Array
(
[id] => 1397126750614479
[email] => my_name#gmail.com
[first_name] => Name
[gender] => female
Etc
As i am using the same user (my account) in both apps, why are there 2 different ID for the same user account?
Thanks
Nat
I am following the example
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2
I have this working and I can get the user..
FacebookSession::setDefaultApplication(FB_APP_ID,FB_APP_SECRET);
$helper = new FacebookRedirectLoginHelper( 'http://localhost:8088' );
$session = new FacebookSession( $_SESSION['fb_access_token'] );
if ( isset( $session ) ) {
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$user = $response->getGraphObject(GraphUser::className());
Now i need to do a fql and i am unsure how to the $facebook object??
$fql = 'SELECT name,page_id FROM page WHERE page_id IN
(SELECT page_id FROM page_admin WHERE uid='.$user->getID() .')';
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
Thanks
Don't use FQL. It been deprecated and will stop working. Use the Graph API instead and do /me/accounts as explained at https://developers.facebook.com/docs/graph-api/reference/v2.2/user/accounts
Here is what I'm seeing in the docs:
Apps can send notifications to any existing user that has authorized the app. No special or extended permission is required.
Okay, sounds good. I'm using the JS SDK and here is what I'm trying to do:
FB.api('/me/notifications?access_token=' + window.accessToken + '&href=test&template=test', function(response) {
console.log(response);
});
This is what I'm getting:
"(#200) The "manage_notifications" permission is required in order to query the user's notifications."
I have tried replacing the href parameter with my app's real domain. Using my facebook ID instead of "/me/" makes no difference either. HELP!
I HAVE tried adding the manage_notifications permission (and still doesn't work...), but my question is why does it say the opposite in the docs?
EDIT: Went to PHP:
<?php
include_once('sdk/facebook.php');
$user = $_POST['user'];
$message = $_POST['message'];
$config = array();
$config['appId'] = '609802022365238';
$config['secret'] = '71afdf0dcbb5f00739cfaf4aff4301e7';
$facebook = new Facebook($config);
$facebook->setAccessToken($config['appId'].'|'.$config['secret']);
$href = 'href';
$params = array(
'href' => $href,
'template' => $message,
);
$facebook->api('/' . $user . '/notifications/', 'POST', $params);
?>
EDIT 2: After a silly logic mistake it now works :)
To send a notification you must use application access token - appid|appsecret, so you should send it server side and execute via AJAX call. PHP example:
require_once("facebook.php");
$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$facebook = new Facebook($config);
$facebook->setAccessToken($config['appId'].'|'.$config['secret']);
$user = 'userid';
$message = 'message';
$href = 'href';
$params = array(
'href' => $href,
'template' => $message,
);
$facebook->api('/' . $user . '/notifications/', 'post', $params);
https://developers.facebook.com/docs/concepts/notifications/
I'm trying to get friends list and I get this error:
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /server/url/base_facebook.php on line 970
This is the code I'm using:
$facebook = new Facebook(array(
'appId' => $appID,
'secret' => $appSecret,
'cookie' => true, // enable optional cookie support
));
$result = $facebook->api('/me/friends/',array('access_token' => $facebook->access_token));
Here's the data I get back in the $facebook variable:
Facebook Object
(
[appId:protected] => 220........
[apiSecret:protected] => 2162e6c1b771......
[user:protected] =>
[signedRequest:protected] =>
[state:protected] => 894ad3b36c2ebdcbcf6d4f110641dd4f
[accessToken:protected] =>
[fileUploadSupport:protected] =>
)
For some reason the access_token is empty.
It worked fine and suddenly stopped...
The app already has the permissions and I'm using the latest PHP SDK v3.0.1
Please help me... I'm going crazy with this issue...
Thanks,
Bar.
it seems like u need to parse the signed request first is this an iframe app or a website?
if it's an iframe app and the user already authorized
try this.
public function parse_signed_request($signed_request)
{
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = $this->base64_url_decode($encoded_sig);
$data = json_decode($this->base64_url_decode($payload), true);
if(strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
throw new Exception('bad algorithm');
}
$expected_sig = hash_hmac('sha256', $payload, $this->app_secret, $raw = true);
if($sig !== $expected_sig) {
throw new Exception('Bad signed JSON');
}
return $data;
}
this will parse the signed request.
after u get the data var_dump it and in it u will see the oauth_token....
private function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
You should add $this->load->library('facebook'); in constructor .
You need to upgrade your SDK to v3.0:
https://github.com/facebook/php-sdk
Edit: Since you are using v.3.0 your construct is wrong.. there is no more $cookie parameter for constructor. I suggest revising documentation.
I'm using the Facebook Graph API and want to check if a user has authenticated my Facebook app by user ID. How do I do this?
You use:
SELECT is_app_user FROM user WHERE uid=USER_ID
This should return:
[
{
"is_app_user": true
}
]
If the user has logged in to your application.
Expanding on ifaour's answer, in PHP this query would look something like this:
<?php
$facebook = new Facebook(
'appID' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET
);
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
));
$is_installed = $result[0]['is_app_user'];
Here you can batch multiple requests together and avoid using FQL.
Assuming you have already logged into facebook and set the access token to the application access token, you can do this:
$batch = array();
foreach($friendArray AS $friend) {
$batch[] = array(
'method' => 'GET',
'relative_url' => '/' . $friend . '?fields=installed'
);
}
FB()->useApplicationAccessToken();
$batchResponse = FB()->facebook()->api('?batch='.json_encode($batch), 'POST');
Then you can process the batch response with code like this:
$installedUsers = array();
$notInstalledUsers = array();
foreach ($batchResponse AS $response) {
$body = json_decode($response['body'], true);
if (!isset($body['id']))
continue;
$id = $body['id'];
if (isset($body['installed']))
$installedUsers[] = $id;
else
$notInstalledUsers[] = $id;
}