I just need simple and working facebook app. I bought ssl for this and i dont want too much expenses.
If user allowed app it will write his name and gender and his/her friends gender.
my app doesnt redirect after user allow the app. I cant fix it so just need basic app with out url redirecting.
ı used this code but it doesnt work. it need to be corrected:
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => '***',
'secret' => '***',
'scope' => 'manage_pages,offline_access,publish_stream,user_photos'
));
$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) {
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Here is the simple one page application full fills your needs
make the file with name index.php
download the facebook library download here
<?php
require_once 'library/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'appid',
'secret' => 'secret',
'cookie' => true,
));
$app_id = 'appid';
$canvas_page = "canvas_page_link";
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . ("&scope=email,read_stream&response_type=token");
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
//getting the userid and some other data for verification
//get the user id
$UserId = $data["user_id"];
echo 'UserId;' . $UserId;
//get the user access token
$token = $facebook->getAccessToken();
echo "</br>" . 'Access_Token:' . $token;
//set default access token and profile
$facebook->setAccessToken($token);
$user_profile = $facebook->api('/me');
//get the user name
$user_id = $facebook->getUser();
$user_profile = $facebook->api('/me','GET');
$user_name = $user_profile['name'];
echo "Name: " . $user_name;
$user_gender = $user_profile['gender'];
echo "Gender: " . $user_gender;
}
?>
Related
Years ago I've developed apps for Facebook, but now seems not working old method to get user's information (id, name, etc) and post data on user's wall.
I've used like this, but for now It returns me blank screen:
require_once('FacebookAPI/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user)
{
try
{
#Code below to post data on user's wall
# Photo Caption
$photoCaption = 'Just used app';
$imageUrl = ('http://example.com'); // Example URL
$link = ('http://www.facebook.com/page/app_xxxx?ref=ts');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'Caption text'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
?>
#Code below to show app
<html>
<body bgcolor="#ffffff">
<center>
// link to my app
</center>
</body>
</html>
// Code below to get user's information (name, id)
<?php
$user_profile = $facebook->api('/me');
$coded = $_REQUEST['code'];
$name = $user_profile['name'];
$id = $user_profile['id'];
session_start();
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
}
catch (FacebookApiException $e)
{
$user = null;
error_log($e);
}
}
else
{ # Code below to get access from user
$redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
Could someone help me out to get successfully user's name, id?
You can try this code. Hope you have downloaded latest php-sdk, you have appid,app des
include_once "php-sdk/src/facebook.php";
$app_id = 'your app id';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
$me = $facebook->api('/me');
$name= $me['name'];
$fid=$me['id'];
}
else
{
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
I'm implementing Facebook login on my website, it's working without error but ultimately I'm not able to get any information of a user.
I've following code.
<?php
include 'library.php';
include 'facebook.php';
$app_id = "XXXXXXXXX";
$app_secret = "XXXXXXXXX";
$site_url = "XXXXXXXXX";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fname = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
echo $loginUrl = $facebook->getLoginUrl( array('scope' => 'email,read_stream'));
What's wrong with this? Can anyone point out? I've gone though a number of questions but none of them seemed to work for me & I finally ended up posting a question myself.
EDIT
When asking for login, it displays permissions correctly like email, friend list, your likes etc. So basically it's asking correctly & even though I approve, information is not coming up. Tried with a couple of users.
EDIT
Try use this code, and work from there ... That's how I figured it out
<HTML>
<BODY>
<?php
require_once("fb/facebook.php");
$config = array(
'appId' => 'XXXXX',
'secret' => 'XXXXXXX',
'fileUpload' => false, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
$loginParams = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($loginParams);
$logoutParams = array('next' => 'http://fb_logouturi');
$logoutUrl = $facebook->getLogoutUrl($logoutParams);
echo $uid . '</br>';
echo 'Login:' . $loginUrl . '</br>';
echo 'Logout:' . $logoutUrl . '</br>';
if($uid) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "</br>First Name: " . $user_profile['first_name'];
echo "</br>Last Name: " . $user_profile['last_name'];
echo "</br>Email: " . $user_profile['email'];
echo "</br>Gender: " . $user_profile['gender'];
echo "</br><img src='http://graph.facebook.com/" . $uid ."/picture?type=large'>";
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</BODY>
</HTML>
I have a problem when using facebook authentication.
<?php
$user = null;
try{
include_once "fb/facebook.php";
}
catch(Exception $o){
error_log($o);
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true
));
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream,email',
//'redirect_uri' => 'https://www.facebook.com/pages/MediaEngine/304221192963546?id=304221192963546&sk=app_143090482526977'
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
$permissions = $facebook->api('/me/permissions');
}catch(FacebookException $e){
}
}else{
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>
When the user accepted the app, it will redirect user to tab page(http://apps4you.hu), user wont stay on facebook, but only 1st time will do that if the user close and come back to app on facebook(already authenticated) it works fine in iframe.
If I use the redirect_uri => my_app , it will redirect and redirect and redirect but never will work the app, the browser just loading and loanding but nothing happends.
I spent days to fix this problem but I could not find any solution.
Any idea whats wrong?
Change $loginUrl as below and try:
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$loginUrl _url = "https://www.facebook.com/dialog/oauth?client_id="
. APP_ID . "&redirect_uri=" . urlencode($next) . "&state="
. $_SESSION['state'] . "&scope=".$permissions;
I need to create a system that when the cliente publish something in his website it will be published in his facebook, but to do this i need to get the access token because the account need to still logged to my system.
I made my app and tested it in my own facebook and when i try to login by my system it work, but when i try to login with the system in the client facebook, return this error:
An error has occurred. Try again later.
My code to generate the AccessToken is this:
require_once("php-sdk/src/facebook.php"); //Up-to-date SDK files from Git
$app_id = "APP_ID"; //ID do APP
$app_secret = "APP_SECRET"; //Secret do APP
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));
$page_id = 'PAGE_ID';
$message = "Teste de integração";
$token_url = 'https://graph.facebook.com/oauth/access_token?'
. 'client_id=' . $app_id
. '&client_secret=' . $app_secret
. '&grant_type=client_credentials';
$token_response = file_get_contents_curl($token_url);
$params = null;
parse_str($token_response, $params);
$app_access_token = $params['access_token'];
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_info = $facebook->api("/$page_id/?fields=access_token");
//print_r($page_info);
if( !empty($page_info['access_token']) ) {
//if(!empty($app_access_token)) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => $message
);
// print_r($args);
// $post_id = $facebook->api("/$page_id/feed","post",$args);
} else {
$permissions = $facebook->api("/me/permissions");
if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
!array_key_exists('manage_pages', $permissions['data'][0])) {
// We don't have one of the permissions
// Alert the admin or ask for the permission!
header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
}
}
} 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(array('scope'=>'manage_pages,publish_stream, offline_access'));
}
?>
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>
I already try to reconfig the user and all the config from the user is the same from the another account that didnt return the error.
I am developing my first facebook app, which includes creating a new album and posting photos to the user wall. By learning through facebook documentation and few tutorial and came up this code, but I am getting following error with it.
Fatal error: Uncaught OAuthException: Error validating application. Thrown in /home/base_facebook.php on line 1106
Plus: I don’t know if it matters or not, but when I am printing out the
echo $facebook->getUser();
variable it giving me ‘0’ in return even though user (which in this case is myself) is logged in.
Kindly help me through this.
Code:
$config = array(
'appId' => '3663250024856',
'secret' => 'b14ac6d2b7dbdtyb259599b06983e881',
'fileUpload' => true,
'cookie' => true
);
$facebook = new Facebook($config);
echo "USER STATUS:".$facebook->getUser();
$facebook -> setFileUploadSupport(true);
$album_details = array('message' => 'Album desc', 'name' => 'Album name');
$create_album = $facebook -> api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
$photo_details = array('message' => 'Photo message');
$file = "temp/".$imageName;
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook -> api('/' . $album_uid . '/photos', 'post', $photo_details);
The best working solution I've found in the web. Add the below code at the top of your php page:
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_URL";
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = #file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
}
Even though the user is logged in, the user needs to give permission to your app before you can make api calls.
Basically the flow for user authentication using facebook php sdk should be like this:
Detect whether the user is logged in and given permission to your app.
If he hasn't given permission, redirect him to the login and application permission page. After logging in and giving permission, he'll be redirected back again to your application page.
Otherwise, show the content of your app, make api calls,...
Here is a modified sample code from the php sdk's documentation page to handle authentication that should work if you just modify the appId, appSecret and redirect_uri:
<?php
require_once 'fb_php_sdk/facebook.php';
$appId = 'your_app_id';
$appSecret = 'your_app_secret';
// Create your Application instance (replace this with your appId and secret).
$facebook = new Facebook(
array(
'appId' => $appId,
'secret' => $appSecret,
'fileUpload' => true,
'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(
array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => '',//these are the extended permissions you will need for your app, add/remove according to your requirement
'redirect_uri' => 'http://apps.facebook.com/test_app_azk/',//this is the redirect uri where user will be redirected after allow,
));
}
if ($user)
{
//show the content of your app
//make api calls
var_dump($user_profile);
}
else
{
//redirect user to loginUrl
echo "<script>parent.location = '".$loginUrl."';</script>";
}
Hope this helps.
The problem might lie within the else statement. Instead of using else use if(!user). That should fix the issue.