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

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]"

Related

What permission do I need to use the notifications API?

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.

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 .

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);

Access Token not retrieving data from Facebook PHP sdk

Here's my code to retrieve user's info from Facebook. I am using PHP SDK.
public function indexAction()
{
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'default_graph_version' => 'v2.5',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
if(isset($_SESSION['facebook_access_token'])){
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
// echo 'Name: ' . $user['first_name'];
$this->_redirectUrl('/app-new/app/#/myAccount');
exit;
}
//redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl(Mage::getBaseUrl().'facebook/facebook/authenticate', $permissions);
echo $loginUrl;
}
public function authenticateAction(){
Mage::log("Authenticate=======");
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'default_graph_version' => 'v2.5',
]);
$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!
Mage::log("Access Token=================>".(string) $accessToken);
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$fb->setDefaultAccessToken((string) $accessToken);
$_SESSION['facebook_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
$customer = $this->checkIfUserExists($userNode);
$this->_redirectUrl('/app-new/app/#/myAccount');
exit;
} 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;
}
}
}
I got the result first, but I am not getting result after that. Is it due to access taken or something else. What am I doing wrong here
You are missing a new feature of v2.4 of the Graph API, it´s called "Declarative Fields". You now have to add a field parameter to specify the fields you want to get, else you will only get id and name: https://developers.facebook.com/docs/apps/changelog#v2_4
Also, make sure your login works correctly and you get asked for the email permission when you authorize your App.

Graph returned an error: (#200) Permissions error

when I publish a post to my page, it's working fine, but when I post to page through set page ID instead of "me",
the response as the following:
Graph returned an error: (#200) Permissions error
the source code as the following:
require_once 'src/Facebook/autoload.php';
//*
$config = array();
$config['appId'] = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_id'] = $config['appId'];
$config['secret'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$config['app_secret'] = $config['secret'];
define('APP_ID', $config['appId']);
define('APP_SECRET', $config['secret']);
//$config['fileUpload'] = false; // optional
$config['default_graph_version'] = 'v2.5';
$config['page_id'] = 'xxxxxxxxxxxxxxxxxxxxxxxx';
$config['facebook']['permissions'] = array(
'email',
'user_location',
'user_birthday',
'publish_actions',
'publish_pages',
'manage_pages',
'public_profile',
);
$fb = new Facebook\Facebook($config);
if(isset($_SESSION['fb_access_token'])) {
$accessToken = $_SESSION['fb_access_token'];
} else {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
if(isset($accessToken)) {
$oAuth2Client = $fb->getOAuth2Client();
// longlived access token
if (!$accessToken->isLongLived()) {
$accessToken = $oAuth2Client->getLongLivedAccessToken($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;
}
}
if(isset($accessToken)) {
// Logged in!
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/'.$config['page_id'].'/feed', $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;
}
} else {
$helper = $fb->getRedirectLoginHelper();
$redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$loginUrl = $helper->getLoginUrl($redirect_url, [ 'manage_pages', 'publish_pages', 'publish_actions' ]);
echo 'Log in with Facebook!';
}
on other action in same page
$page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// instance
$fb = new Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => encodeurl(array('page'=>'page','lang'=>'ar','id'=>$_id,'beg'=>0,'unuse'=>$xtitlex)),
'message' => $xtitlex,
'source' => $publish,//$fb->fileToUpload($publish),
'caption' => DOMAIN_,
"picture" => $publish,
"name" => $xtitlex,
"description" => $xdescriptionx
];
try {
//
$response = $fb->post('/'.$config['page_id'].'/feed', $linkData, $page_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//print_r($linkData);
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
Most likely, you are not using a Page Token with the correct permissions. Make sure you are using a Page Token instead of a User Token and make sure it includes publish_pages. You can test your Token in the debugger: https://developers.facebook.com/tools/debug/
Make sure the Page ID shows up, and the publish_pages permission.
First check if the Access Token you used is generated as "Page Access Token" not "User Token". It is a bit tricky and took me a moment.
go to "https://developers.facebook.com/tools/explorer/"
drop down "User Token" menu
pick "Page Access Token"
blue button "Generate Access Token"
copy and use new token
Every time you add or remove Permissions, token changes!
Screenshot
My app types was business and I just added all the permissions from the given list and after I added all permissions, Facebook asked me to review permissions, I simply kept clicking on OK and everything worked, the error went

Categories