This is my code:
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '25856*****93875',
'secret' => '7c3e95185d681ee6****8bcef1305352',
'cookie' => true
));
$user = $facebook->getUser();
if($user) {
try {
$statusUpdate = $facebook->api('/me/feed', 'post', array('link' => 'http://www.firsatdergisi.com/'));
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
else
{
$url = 'https://www.facebook.com/dialog/oauth?client_id=25856*****93875&redirect_uri=http://apps.facebook.com/gunlukburcpaylas/&scope=email,read_stream,publish_stream';
echo "<a href='". $url ."'>login</a>";
}
?>
Error: (#100) The post's links must direct to the application's connect or canvas URL.
what i should?
The error appears to tell you all you need to know - you can only post a link to your application's connect or canvas url, not directly to an OAuth dialog.
Related
I use facebook-php-sdk-v3.2 to upload video from my website to a Facebook account.
This is my code:
<?php
require 'src/facebook.php';
$file = "movie.mp4";
$post_type = '/me/videos';
$post_params = array(
'access_token' => '***************************',
'title' => 'title of the image',
'source' => '#' . $file,
);
$facebook = new Facebook(array(
'appId' => '1061864210567772',
'secret' => '***************************'
));
print $facebook->getUser();
$facebook->setFileUploadSupport(true);
try{
$fbpost = $facebook->api($post_type, 'POST', $post_params);
print($fbpost);
} catch (Exception $e) {
echo $e->getMessage();
}
This question is already asked before and I saw all of them, but none of them worked for me. The output of this code is 0
$uid = $facebook->getUser();
But the error is
An active access token must be used to query information about the current user
I am trying to update Facebook user status using Graph api. My code is
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' =>'389694921095423',
'secret' =>'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'cookie' => true
));
$access_token = $facebook->getAccessToken();
echo($access_token);
$me = null;
try
{
$uid = $facebook->getUser();
$me = $facebook->api('/me');
echo "Welcome User: " . $me['name'] . "<br />";
//access permission
$permissions_needed = array('publish_stream', 'read_stream', 'offline_access', 'manage_pages');
foreach($permissions_needed as $perm)
{
if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 )
{
$login_url_params = array(
'scope' => 'publish_stream,read_stream,offline_access,manage_pages',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
//Access permission
$post_id = $facebook->api("/$uid/feed", "post", array("message"=>"Hello World!"));
if(isset($post_id))
{
echo "A new post to your wall has been posted with id: $post_id";
}
}
catch (FacebookApiException $e)
{
echo($e);
}
?>
The problem is that it shows a run time error like Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. I have generated the Access Tocken. Where i have to use that to remove this exception
Thanks in advance
Try changing the line:
$me = $facebook->api('/me');
to
$me = $facebook->api('/'.$uid);
Also try upgrading your sdk if you are not using the latest version and if you just upgraded the sdk and it caused the trouble, than you can try to downgrade the version and check.
Clear your cache & cookies (if you are testing or developing application)
and
Please do as follow:-
try
{
$fb_user_info = $facebook->api('/me');
} catch (Exception $ex)
{
$facebook->destroySession();
$params = array(
'scope' => 'YOUR_PERMISSIONS',
'redirect_uri' => 'YOUR_SITE_URL'
);
$fb_login_url = $facebook->getLoginUrl($params);
header("Location:" . $fb_login_url);
}
I'm using the Facebook openGraph API for PHP. Here is my code:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
break;
}
$fbconfig['appid' ] = "APPID";
$fbconfig['secret'] = "SECRET";
$fbconfig['baseurl'] = "http://blah.com/";
$user = null;
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
try {
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
} catch (Exception $e) { echo "ERROR: ".$e; }
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,read_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl'].'blah.php'
)
);
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ($fbconfig['baseurl'].'blah.php') ));
//if user is logged in and session is valid.
if ($user ){
header("Location;$loginUrl");
break;
}
?>
Now, this works on other pages (the Facebook part) but on this page only (same dir as other pages) The browser thinks that the PHP ends at "'appId' =>" and from then on interprets it as html. So I get this in my browser:
http://cl.ly/image/1y2a2F0e3I35
Sorry for the weird cropped picture, but I can't divulge the name or intent of the business.
There is HTML after the PHP on the real site and I can give this + js if needed.
Thank you!
your header is wrong.......try following......
header("Location:$loginUrl");
and also if you don't get facebook user id then you have to redirect to login page but you did reverse.may do following
//if user is not loggin
if (empty($user)){
header("Location:$loginUrl");
}else{
try {
$userInfo = $facebook->api("/$user");
} catch (FacebookApiException $e) {
$userInfo=null;
}
}
Trying to post a status update using facebook php sdk. Code posted below. As I understand it, I need ask for extended persmission, which I do in the login URL with '&scope=publish_stream', but this still isn't working.
The resultant login URL looks like:
https://www.facebook.com/login.php?api_key=174954539217971&cancel_url=http%3A%2F%2Fexample.com%2facebook.post.php&display=page&fbconnect=1&next=http%3A%2F%2Fexample.com%2Ffacebook.post.php&return_session=1&session_version=3&v=1.0&scope=publish_stream
So you can see it is being set. While this is essentially the same question as: Facebook OAuthException: "user hasn't authorized the application to perform this action"
The suggested solution in that thread is not working here.
<?php
echo "Posting to facebook..<br/>";
require './fb_src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
error_log($e);
}
}
else {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
}
print_r($me);
try {
$feed = $facebook->api('/me/feed', 'post', array('message' => 'Hello world!', 'cb' => ''));
} catch (FacebookApiException $e) {
print($e);
}
print_r($feed);
?>
parameter is 'req_perms' not 'scope'. So hard to find good documentation...
Works fine now.
I have developed a Facebook application that runs inside an iframe in the Facebook canvas. For it to work properly I request extended permissions from the user. If the user hasn't authorized the application I send him/her to a login page with the getLoginUrl() method in the PHP SDK.
It works, but it's not pretty. The method sends the user to a landing page before the authentication page. It looks like this:
When I click "Go to Facebook.com" I see the actual page for permission requests (I also get right to the permissions page if I print the url, copy it and enter it into a new browser window). How do I make Facebook skip this step when I do the redirect from an Iframe?
My code looks like this (using CodeIgniter and Facebook PHP SDK):
$this->facebook = new Facebook(array(
'appId' => '{MY_APP_ID}',
'secret' => '{MY_SECRET}',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$this->facebook->getSession();
try {
$this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
$this->me = NULL;
}
if ( is_null($this->me) ) {
redirect($this->facebook->getLoginUrl(array(
'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
)));
}
I think you need to redirect the parent frame (i.e. _top) rather than the iFrame itself?
The way I do it is set up an INDEX.PHP file with the following
//if user is logged in and session is valid.
if ($fbme){
//fql query example using legacy method call and passing
parameter
try{
$fql = "select name, hometown_location, sex,
pic_square from user where uid=" .
$uid;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
Then point your canvas url to http://yoursite.com/INDEX.php
The callback url in the above code which will be in INDEX.PHP sets where to look after permissions are granted.
FBMain.php looks like this
//set application urls here
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']
= "http://www.tyoursite.com/YOURMAINPAGE.php/";
$fbconfig['http://apps.facebook.com/CANVASBASEURL']
= "http://apps.facebook.com/CANVASBASEURL";
$uid = null; //facebook user id
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['APPID'],
'secret' => $fbconfig['SECRET'],
'cookie' => true,
));
//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms'=>'email,publish_stream,status_update,user_birthday,user_location'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
} ?>
Hope its a little clearer. It took me a while to figure it out, but I got there, thought I would help.