I've got a php page that is pulling events from a facebook fan page. If I open an incognito window (or log out of facebook) and then click the link, a blank page opens. My guess is there's something to do with access tokens, permissions of the FB app and permissions to the page. Any insight would be greatly appreciated.
The ticket_uri tries to open a link like this: http://www.facebook.com/ajax/events/ticket.php?event_id=147884225370550&action_source=12. If logged in, it seems like this is auto-forwarded to the actual ticket link in the event.
Here is page I'm working on: http://danagould.com/live.php. It's pulling from the facebook page officialdanagould
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<?php
date_default_timezone_set('America/Los_Angeles');
//requiring FB PHP SDK
require 'fb-sdk/src/facebook.php';
//initializing keys
$facebook = new Facebook(array(
'appId' => 'MYAPPID',
'secret' => 'MYAPPSECRET',
'cookie' => true, // enable optional cookie support
));
$fql = "SELECT name, pic, start_time, end_time, location, description, eid, has_profile_pic, venue, ticket_uri
FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = 175890419184371 AND start_time > now() )
ORDER BY start_time asc";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
//looping through retrieved data
foreach( $fqlResult as $keys => $values ){
$convertedtime = strtotime($values['start_time']);
$start_date = date( 'F j', $convertedtime );
$content = $values['description'];
//printing the data
echo "<tr>";
echo "<td width='8%'>";
if ( $values['has_profile_pic'] == FALSE) {
echo "<img src=img/spacer.gif width='100%'/>";
} else {
echo "<img src={$values['pic']} width='100%'/>";
}
echo "</td>";
echo "<td width='2%'></td>";
echo "<td width='18%' class='tourdate'>{$start_date}</td>";
echo "<td width='49%'>";
echo "<table><tbody><tr><td class='tourlocation'>{$values['name']}</td></tr>";
echo "<tr><td class='poddesc'><p class='poddesc'>At {$values['location']}</p></td></tr></tbody></table>";
echo "<td width='23%' class='getickets'>";
echo "Get Tickets";
echo "</td></tr>";
echo "<tr height='5px'></tr>";
}
?>
</tbody>
</table>
Try setting the cookies to false or set $fql globally. In addition what are you stating in callback
Related
I am following Justin Slope's API Tutorial which was made in 2020.
This is the code that I pasted with some changes on the redirect URLs and using a site that is ok with the developer website.
$creds = array(
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v3.2',
'persistent_data_handler' => 'session'
);
// create facebook object
$facebook = new Facebook\Facebook( $creds );
// helper
$helper = $facebook->getRedirectLoginHelper();
// oauth object
$oAuth2Client = $facebook->getOAuth2Client();
if ( isset( $_GET['code'] ) ) { // get access token
try {
$accessToken = $helper->getAccessToken();
} catch ( Facebook\Exceptions\FacebookResponseException $e ) { // graph error
echo 'Graph returned an error ' . $e->getMessage;
} catch ( Facebook\Exceptions\FacebookSDKException $e ) { // validation error
echo 'Facebook SDK returned an error ' . $e->getMessage;
}
if ( !$accessToken->isLongLived() ) { // exchange short for long
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken( $accessToken );
} catch ( Facebook\Exceptions\FacebookSDKException $e ) {
echo 'Error getting long lived access token ' . $e->getMessage();
}
}
echo '<pre>';
var_dump( $accessToken );
$accessToken = (string) $accessToken;
echo '<h1>Long Lived Access Token</h1>';
print_r( $accessToken );
} else { // display login url
$permissions = [
'public_profile',
'instagram_basic',
'pages_show_list',
'instagram_manage_insights',
'instagram_manage_comments',
'manage_pages',
'ads_management',
'business_management',
'instagram_content_publish',
'pages_read_engagement'
];
$loginUrl = $helper->getLoginUrl( FACEBOOK_REDIRECT_URI, $permissions );
echo '<a href="' . $loginUrl . '">
Login With Facebook
</a>';
}
Whenever I try to use the code this is always the output been trying for about an hour by the time this is posted
I tried changing the graph versions and double-checking redirects and I am still stumped on this.
Had to remove almost all of the stuff on the permissions 'pages_show_list' was the only thing left since the data isn't supported on the standard access on the Facebook developer website, have to get a privacy URL to get full access to all the other permissions.
http://apps.facebook.com/karmabalance/
I am trying to get the login and authorization working on my FB App, but it seems to do funny stuff. Basically all I want is when the user clicks on my app, I want fb to do their check to make sure they are logged in and have approved the app, and after that I need the program to do a check of the database to see if their is already a record in the database with the User FB ID. If their is no record, I want the program to send the user to a create acct. page that will enable the user to enter a Username. If there is a record, I want to send the user to the app home page.
I thought I got it to work, but its still not right, and I can't figure out why. Here is my code. I'm sure there is a better way to do this also, I am a newbie, so I'm trying to figure this all out as I go. Any help would be appreciated.
Thanks
When the user clicks my app it goes to this index.php page.
<?php
require 'config.php';
session_start();
$usercheck = $user_profile['id'];
$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");
if ($facebook->getUser())
{
if(mysql_num_rows($result))
{
header('location: Home.php');
} else
{ ?>
<p>Please choose a Username for yourself. </p>
<form action='includes/signup.php' method='post'>
<fieldset style="width:600px";>
<label>Username</label>
<input name='username' type='text' value='<? if ($facebook->getUser())
{echo'Choose a Username';} ?>' />
<input name='submit' type='submit' value='submit' />
</fieldset>
</form>
<? }
} else
{ ?>
<p>Sign up with Facebook <fb:login-button perms='email'> Connect</fb:login-button>
It only takes a few seconds</p>
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<script>
FB.init({
appId:'334230339967350', cookie:true,
status:true, xfbml:true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload(); //will reload your page you are on
});
</script>
<? }
?>
The config file:
<?php
//Facebook Configuration
require 'facebook-php-sdk/src/facebook.php';
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
//SQL Configuration
// i start most if not all pages with this depending on what Im using it for.
$host = 'localhost'; // host name OR IP
$username = 'security';//username
$pass = 'blockedout'; //password
$dbname = 'Security'; // Database Name
$conn = mysql_connect($host, $username, $pass) or die(mysql_error());
if ($conn)
{
mysql_select_db($dbname) or die(mysql_error());
}
else
{
echo 'Connection failed.';
} // The above code connects to your database or errors if it cannot connect.
// Again this is simple, security is your own priority.
//GLOBAL VALUES??
$usercheck = $user_profile['id'];
$usernamequery = mysql_query("SELECT UserName FROM PLAYER WHERE (FBID = '$usercheck') ");
$username = mysql_fetch_array($usernamequery);
$levelquery = mysql_query("SELECT LevelID FROM PLAYER WHERE (FBID = '$usercheck') ");
$level = mysql_fetch_array($levelquery);
$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");
?>
And here is the signup.php page
<?
require_once '../config.php';
//here you could add checks for any empty fields using (!($_POST['first_name']))
$first_name = $user_profile['first_name']; // this line will collect our information from the
// field in our form that has the facebook first_name in it.
$last_name = $user_profile['last_name']; // same as above
$email = $user_profile['email']; //same as above
$id = $user_profile['id']; //same as above
$username1 = $_POST['username'];
$query = mysql_query
("
INSERT INTO PLAYER (FirstName, LastName, EMail, FBID, UserName, Status, LevelID, Cash, LifePoints, RespectPoints, ReputationPoints, UpgradePoints, HealthPercent)
VALUES ('$first_name', '$last_name', '$email', '$id', '$username1', '1', '1', '25000', '3', '3', '3', '20', '100')
")
or die(mysql_error());
// The query will insert our fields in to the database as the above line shows, make
//sure your database table headers are exactly correct otherwise this will not work
// You can now either send an email or if you wanted header to a new page. This is
//up to you. Tutorials on google will show you how to do this part
if($query){
header('location: ../Home.php');
}else {
echo 'error adding to database';
}
?>
You are missing the oauth token for the user. thats the handle of the user you want to pull.
if you are still confuse, let me know.
I want to make further use of the API by obtaining the list of our user's friends and displaying their details in my application.
in old version, I use
$facebook->api_client->friends_get and
$facebook->api_client->users_getInfoBut in facebook 2012,i don't know how to use?
some body help me.
sample:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('src/facebook.php');
$config = array(
'appId' => '*******',
'secret' => '************',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if(empty($user_id)){
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $GLOBALS['config']['appid'] . "&redirect_uri=" . urlencode($my_url)."&scope=friends_hometown";
echo ("<script> top.location.href='".$dialog_url ."' </script>");
}
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$fql = 'SELECT pic_square,hometown_location,name,sex FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())';
$ret_obj = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
$count=count($ret_obj);
echo '<pre>';
for($i=0;$i<$count;$i++){
echo 'Name: ' . $ret_obj[$i]['name'];
echo '<br />';
echo 'Sex:' . $ret_obj[$i]['sex'];
echo '<br />';
echo 'Home :' . $ret_obj[$i]['hometown_location']['city'];
echo '<br />';
}
// FQL queries return the results in an array, so we have
// to get the user's name from the first element in the array.
echo'</pre>';
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
}
?>
</body>
</html>
you can use php Sdk
& to get Friends of users
use this code
$friends=$facebook->api('me/friends/,'GET');
& about information it depends on what information you want if you just want name & other very public infos the code above is enough
I have an application on Facebook where I select a picture and I can upload to an album associated with the application.
I found some code about it, but until now, I could upload only to my personal account.
With this code I can get all the album details of the application:
$facebook->api('/app_id/albums?access_token='.$acces_token);
That's cool, I get a list, but after this, I do the following:
$upload_photo = $facebook->api('/'.$album_id.'/photos?access_token='.$acces_token, 'post', $photo_details);
And I always get the following exeption:
Fatal error: Uncaught OAuthException: (#100) Invalid ID for album owner thrown in
What is the problem?!
Maybe I don't have the right permissions? Or am I missing something?
I even tried this:
$create_album = $facebook->api('/app_id/albums?access_token='.$acces, 'post', $album_details);
$album_uid = $create_album['id'];
$upload_photo = $facebook->api('/'.$album_uid.'/photos?access_token='.$acces, 'post', $photo_details);
Try this!! it creates a new album and then it posts a photo in that specific album..
$post_login_url = "http://apps.facebook.com/yourapp/thefile";
$album_name = 'name of Album';
$album_description = 'something about album';
$code = $_REQUEST["code"];
$facebook->setFileUploadSupport(true);
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $facebook->getAccessToken();
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
?>
I'm trying to add a tab to a fanpage using the graph api/PHP SDK and I'm receiving an error :
(#210) Subject must be a page I've tried using both the user access_token AND the page access_token but neither work. I've tried using the page id of numerous accounts and still no go. Here is my code:
<?php
$path="/PAGE_ID/tabs/";
$access_token="ACCESS_TOKEN";
$params = array(
'app_id' => "APP_ID",
'access_token' => $access_token
);
try{
$install = $facebook->api($path, "POST", $params);
}catch (FacebookApiException $o){
print_r($o);
}
?>
And here is the error I get:
FacebookApiException Object
(
[result:protected] => Array
(
[error] => Array
(
[message] => (#210) Subject must be a page.
[type] => OAuthException
)
)
[message:protected] => (#210) Subject must be a page.
[string:Exception:private] =>
[code:protected] => 0
Thanks for any help you can provide!
If you are not limited to using the API to add your application to your page then you can follow the instructions provided by Facebook at this link :
https://developers.facebook.com/docs/reference/dialogs/add_to_page/
Essentially you can use a dialog ( see the link above ) or this direct URL to add tab apps to your page :
https://www.facebook.com/dialog/pagetab?app_id=YOUR_APP_ID&display=popup&next=YOUR_URL
Dont forget to substitute APP_ID for your app id and next for a different URL
API Call is atm bugged: https://developers.connect.facebook.com/bugs/149252845187252?browse=search_4f31da351c4870e34879109
But here is a solution for JS: OAuthException "(#210) Subject must be a page." - just do not use the library and do your own call.
I did it with PHP:
<?php
$url = 'https://graph.facebook.com/<PAGE ID>/tabs?app_id=<APP ID>&method=POST&access_token=<PAGE ACCESS TOKEN>&callback=test';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Echo value should be something like "test(true)".