What permission do I need to use the notifications API? - php

What permissions do I need to get in order to use the notifications API? My test app contains two files:
nfytest.php:
<?php
require_once "../vendor/facebook/graph-sdk/src/Facebook/autoload.php";
session_start(); $_SESSION = array();
echo "<html><body>";
try{
$fb = new Facebook\Facebook(['app_id' => 'xxx','app_secret' => 'xxx', 'default_graph_version' => 'v3.2']);
$helper = $fb->getRedirectLoginHelper();
$loginurl = $helper->getLoginUrl("https://www.bkassist.com/dnamatchmakerdev/callback.php", 'email', 'publish_to_groups']);
echo "<a href='$loginurl'>Pleaseclick on this link</a>";
} catch(Facebook\Exceptions\FacebookResponseException $e) {
error_log($e->getMessage());
echo 'Graph returned an error: '. $e->getMessage();
}catch(Facebook\Exceptions\FacebookSDKException$e){
error_log($e->getMessage());
echo 'Facebook SDK returned anerror: ' . $e->getMessage();
}
echo "</body></html>"; return 200;
?>
callback.php:
<?php
require_once "../vendor/facebook/graph-sdk/src/Facebook/autoload.php";
session_start();
ini_set("error_log", "error.log");
echo "<html><body>";
try
{
$fb = new Facebook\Facebook(['app_id' => 'xxx', 'app_secret' => 'xxx', 'default_graph_version' => 'v3.2']);
$helper = $fb->getRedirectLoginHelper();
$at = $helper->getAccessToken();
$fb->post("/2465651860/notifications", ["template" => "#[2465651860] has posted something you said you were interested in", "href " => "index?nfycallback"], $at);
echo "Success. Or at least not failure.";
}
catch(Facebook\Exceptions\FacebookResponseException $e)
{
error_log($e->getMessage());
echo 'Graph returned an error: ' . $e->getMessage();
}
catch(Facebook\Exceptions\FacebookSDKException $e)
{
error_log($e->getMessage());
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
echo "</body>/</html>";
return 200;
?>
Running this produces the error: Graph returned an error: Unsupported post request. Object with ID '2465651860' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api/
I have done, but I haven't found enlightenment yet. [Sorry about the formatting of the code -- I swear I tried to make it look ok!]

First, I think you should not paste your app info in the question. So change the password to protect your app.
Then, I have do some tests, Only not give the access_token parameter will return this error. So check the the access token parameter?
BTW, This method must be called with an app access_token. and only can send notifications who has installed your app.

Related

Facebook SDK returned an error: Stream returned an empty response in facebook SDK5 php CodeIgniter 3

Last week i installed facebook SDK via composer on CODEIGNITER 3 ,it work fine and return name email and token until this day , When I try login the page loads for 1 minute and then an empty page apears with the error:
Empty Error
Facebook SDK returned an error:
and Return This URI
https://www.mywebsite.com/en/fbcallback?code=AQCCasdfSAD35L-3iABFbT5zntYQ4jJHVcko9ekG-6k-jn5aERodXoasdf7jHWEgSxxqze_K-nyAYSmcASDFaskde6wPasdfD8WHBEFUEO5gNpgLU0RJqnvVGCdYKNfT2Qm5U1pcWCvVE_YkJ6sQyUL0RrcONrqMbb7bpPv0KDUQjaO_XMAwvKEo2Jasdf31PCjxoHBGoogupX8VF5Gx6WaT4b7ZlsYkPhbQPTCdJaWrzwvfJ1So27Wsdt1Ub9WPUO07io3vxmX-P-rmzsxnk3qDSKCPJX9ks0eBtNwXA-83&state=8cbe21da648d832fcd2b34aceb8dfe62#=
im using 2 action in my controller for login :
function fblogin(){
$fb = new Facebook\Facebook([
'app_id' => 'my app id ',
'app_secret' => 'my app secret',
'default_graph_version' => 'v3.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
// For more permissions like user location etc you need to send your application for review
$loginUrl = $helper->getLoginUrl('https://www.example.com/en/fbcallback', $permissions);
header("location: ".$loginUrl);
}
this where it should return token info :
function fbcallback(){
$fb = new Facebook\Facebook([
'app_id' => 'myid',
'app_secret' => 'myscretapp',
'default_graph_version' => 'v3.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
}catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,name,email,first_name,last_name',$accessToken);
// print_r($response);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'ERROR: Graph ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'ERROR: validation fails ' . $e->getMessage();
exit;
}
// User Information Retrival begins................................................
$me = $response->getGraphUser();
echo "First Name: ".$me->getProperty('first_name')."<br>";
echo "Last Name: ".$me->getProperty('last_name')."<br>";
echo "Email: ".$me->getProperty('email')."<br>";
echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>"."<br>";
$profileid = $me->getProperty('id');
echo "</br><img src='//graph.facebook.com/$profileid/picture?type=large'> ";
echo "</br></br>Access Token : </br>".$accessToken;
}
the same question was asked 2 years ago but no answers :
link
My problem was on server side :
This feature allows you to specify certain IPs or IP ranges to which you will be able to open outgoing connections.
Just go to your server and check if it block outgoing connections....
I added it to white list and work fine ! ... hope that can save your time .

login success page is not loading after authentication facebook sdk

i am trying to get get user details from facebook using facebook sdk 5.
login page is loading and asking for login with facebook now when i am clicking on link its redirecting me to facebook and asking to allow share details i am clicking allow and then its redirecting to call back page but problem is callback page is keep loading after some time browser is showing
The connection has timed out
The server at website.com is taking too long to respond.
Below is the screen shot and code i am using
<?php
session_start();
require_once 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxx', // Replace {app-id} with your app id
'app_secret' => 'xxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.2',
]);
$redirect = 'https://website.com/fb2/login.php';
# Create the login helper object
$helper = $fb->getRedirectLoginHelper();
# Get the access token and catch the exceptions if any
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
# If the
if (isset($accessToken)) {
// Logged in!
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
// But we shall we the same page
// Sets the default fallback access token so
// we don't have to pass it to each request
$fb->setDefaultAccessToken($accessToken);
try {
$response = $fb->get('/me?fields=email,name');
$userNode = $response->getGraphUser();
}catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// Print the user Details
echo "Welcome !<br><br>";
echo 'Name: ' . $userNode->getName().'<br>';
echo 'User ID: ' . $userNode->getId().'<br>';
echo 'Email: ' . $userNode->getProperty('email').'<br><br>';
$image = 'https://graph.facebook.com/'.$userNode->getId().'/picture?width=200';
echo "Picture<br>";
echo "<img src='$image' /><br><br>";
}else{
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl($redirect,$permissions);
echo 'Log in with Facebook!';
}
I figured it out it was hosting issue , hosting servers where blocking internal requests which was causing this error.

Facebook SDK v5 Graph returned an error: Invalid OAuth access token

Disclaimer: I am relatively new to php coding and very new to the Facebook SDK.
I have been trying to learn the Facebook API with their online materials and examples and find them to be rather lacking. I am trying to do something very basic - simply login with Facebook and retrieve a few user values.
Following Facebook's example I have login.php which looks like this:
<?php
session_start();
require_once __DIR__ . '/../_fb/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '26777overwritten335',
'app_secret' => '8260906deoverwrittenf286510',
'default_graph_version' => 'v2.6',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://www.domain.com/_mdp/fb_dev/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
...and that seems to work fine.
On the login-callback.php page I have the following:
<?php
session_start();
require_once __DIR__ . '/../_fb/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '267772overwritten335',
'app_secret' => '8260906de2overwritten1f286510',
'default_graph_version' => 'v2.6',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
echo 'You are now logged in!';
echo '<br> the access token is: ' . $accessToken . '<br>';
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
// Am Successfully Logged In Here
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '$accessToken');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
// OR
// echo 'Name: ' . $user->getName();
?>
I do appear to be able to successfully login as I get the echo from the line:
echo 'You are now logged in!';
I have tried so many different examples of code and get various errors but the above code results in being echoed back the following:
Graph returned an error: Invalid OAuth access token.
And I have googled that every way I can think of and have yet to find out what I am doing wrong.
If anyone can show me where I am missing something and hopefully give me some corrected codelines it would certainly be appreciated.
Also, FYI - I really want to use graph version 2.6 with SDK 5 because it appears there are a few edges available that aren't in recent versions.
Thanks so much for any help pointing me in the right direction.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '$accessToken');
You are passing the literal value $accessToken here - and that is of course not a valid access token.
PHP does not parse variables in strings with single quotes. Please go read http://php.net/manual/en/language.types.string.php in that regard, this is basic knowledge you need.
And then, stop putting simple variables into strings altogether.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', $accessToken);

Using the Facebook PHP-SDK, I cannot access individual videos

I am having a weird issue. Every time I try to read a specific video from FB, it tells me:
(#100) type must be one of the following values: tagged, uploaded
Which is odd, as the access token in this case IS ME, and I uploaded the video, so I SHOULD be able to read the data.
when I hit "/me/videos/uploaded" it returns a list of my videos, with the correct id's (I thought maybe I was using the wrong value).
here is the quick and dirty I am using to test
public function fbtestAction() {
$code = $this->params()->fromQuery('code');
$state = $this->params()->fromQuery('state');
$fb = new \Facebook\Facebook([
'app_id' => [add_id],
'app_secret' => [app_secret],
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if(!$code) {
$permissions = ['user_videos'];
$loginUrl = $helper->getLoginUrl('http://test.local/fbtest', $permissions);
die("<a href='$loginUrl'>clickity</a>");
} else {
try {
$accessToken = $helper->getAccessToken();
$fb->setDefaultAccessToken($accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
} finally {
echo '<pre>';
//var_dump($fb->get('/me'));
var_dump($fb->get('/me/videos/uploaded'));
//var_dump($fb->get('/videos/[video_id]'));
echo '</pre>';
die;
}
}
}
`
Reading comprehension for the win. The proper endpoint is "/[video_id]" not "/videos/[video_id]"

posting as a page not as an individual with facebook php sdk 5.0.0

I've searched all over for this, the 'answers' I got were outdated. Anytime I run the script it posts the message with my profile to the facebook page even though I have admin permissions.
This is the code I'm using
<?
require_once __DIR__ . '/Facebook/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4'
]);
$page_id="";
$accessToken='Access token I got from https://developers.facebook.com/tools/explorer/145634995501895/';
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('');
try {
$response = $fb->get('/me');
$userNode = $response->getGraphPage();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo 'Logged in as ' . $userNode->getName();
?>

Categories