I am in the process of devloping a facebook canvas application in php and FBML, and am having issues when it comes to setting up authorization for the application. I basicly do not kno how to go about it, or what the best method is.
I have been searching around on the internet most of the day but either stummble on old API things, or i dont know how to implement what they are explaining.
Here is what i have:
$facebook = new Facebook(array(
'appId' => '*snip*',
'secret' => '*snip*',
'cookie' => true,));
$session = $facebook->getSession();
$fbme = null;
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
d($e);
}
}
if (!$fbme) {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => "publish_stream,user_birthday,friends_birthday,user_events,user_hometown,friends_hometown,user_location,friends_location,offline_access,"
));;
}
if (isset($loginUrl)) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
But that does nothing, and if i try with changing the headers instead of using script block, i get:
The URL ... is not valid
Am i going about this all wrong?
Thanks in advance for any help.
Andy
My common include has the following for authenticating:
$facebook = new Facebook(array(
'appId' => $ini['appid'],
'secret' => $ini['appsecret'],
'cookie' => true,
));
$session = $facebook->getSession();
$fbme = null;
if ($session) {
try {
$fbme = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
}
}
// new login check
if (!$fbme) {
$loginUrl = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email',
'next' => FB_APP_URL,
'cancel_url' => FB_APP_URL,
));
echo '<fb:redirect url="' . $loginUrl . '" />';
}
That basically shows a message prompting the using the either add the application or leave; if they are already authenticated then just proceeds with what's beyong the if (!$fbme) block.
Related
I'm building facebook app. I need to get some permissions. I use php sdk.
Everything works fine, but the problem is that LOGIN URL opens in popup mode and some browsers doesn't allow that action.
Can you tell what can I do in this case?
<?php
require 'src/facebook.php';
$config = array(
'appId' => '123456789',
'secret' => '123456789',
'fileUpload' => true,
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if($user_id) {
try {
include "some.php";
$user_profile = $facebook->api('/me','GET');
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl(array( 'scope' => 'photo_upload', 'scope' => 'user_likes', ));
header ("Location: $login_url");
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl(array( 'scope' => 'photo_upload', 'scope' => 'user_likes', ));
header ("Location: $login_url");
}
?>
I dont seem to be able to get this to work. The event loads fine but the image just doesnt upload, please let me know what I am doing wrong, I thiink there is a problem with the picture url format, does in need the # in front?, I have tried it without and it doesn't work:
////////////////////////////////////////////////////////////////////////////////////////
require 'facebook.php';
// appId and secret all work fine
$facebook = new Facebook(array(
'appId' => '************',
'secret' => '**************',
'fileUpload' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$response = $facebook->api(
"/me/events",
"POST",
array (
'name' => $title,
'start_time' => $start_time,
'location' => $location,
'end_time' => $end_time,
'description' => $description,
'picture' => '#http://andyotto.files.wordpress.com/2013/01/donald- duck.gif?w=863',
'venue' => $venue
)
);
if (count($response)>0){
$response_message = "Event successfully added uploaded to Facebook.";
}else{
$response_message = "There was a problem uploading your event to Facebook.";
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope'=>'create_event'
));
header( 'Location:'.$loginUrl ) ;
}
It seems from the docs that you can't upload an image directly once you publish an Event (https://developers.facebook.com/docs/graph-api/reference/user/events/#publish), at least the picture filed is not described.
According to this doc (https://developers.facebook.com/docs/graph-api/reference/event#picture) you can set the Event's picture once it's created via
POST /{EVENT_ID}/picture
with the picture encoded as "multipart/form-data" in the request's body parameter "source".
I want to use PHP to send message to my Facebook friends. I found some code but it just lets me log in. Could anyone help me?
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXXX',
'cookie' => true,
));
$session=$facebook->getUser();;
if ($session) {
$logoutUrl = $facebook->getLogoutUrl();
$access_token = $facebook->getAccessToken();
try {
$result = $facebook->api(
'me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
1) Did you provided the correct permissions to the app?
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
2) Check this answer: Update facebook status using php and this: Facebook PHP SDK , Post to another users wall whats wrong?
3) it's /me/feed, not me/feed:
$result = $facebook->api(
'/me/feed',
'post',
array('access_token' => $access_token , 'message' => 'test')
);
I am using this code to post to the user's wall :
require 'fb/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'redirect_uri' => 'urlhere',
);
$fb_session = $facebook->getUser();
// Session based API call.
if ($fb_session)
{
try
{
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
}
catch (Exception $e)
{
$fb_login_url = $facebook->getLoginUrl($params);
echo $e->getMessage();
}
}
else
{
$fb_login_url = $facebook->getLoginUrl($params);
}
The code is working till the point uuer clicks on login part to post. After that the url contains a code = xxxzz and a state =yyy but the $fb_session is 0.
This works sometimes without any changes. Please help!
You should include the access token when you try to post to some User Facebook wall.
First get access token:
$fbToken = NULL;
$fbToken = $facebook->getAccessToken();
right after $fb_session = $facebook->getUser();
and then include it in the $args array:
'access_token' => $fbToken
if session is NOT created or access token is not valid, you should redirect the user back to re-authorize your Facebook App.
So in ELSE, you should add
header("Location: $fb_login_url ");
after $fb_login_url = $facebook->getLoginUrl($params);
assuming that everything else is correct, but the access_token is not valid, you should also include header("Location: $fb_login_url "); before echo $e->getMessage();
in both cases you are using getloginurl
shouldn't it be
if ($fb_session) {
try {
$args = array(
'message' => $_GET['by'],
'link' => 'linkhere/',
'caption' => $_GET['test']
);
$post_id = $facebook->api("/me/feed", "post", $args);
} catch (Exception $e) {
$fb_logout_url = $facebook->getLogoutUrl;
.
.
.
I'm "translating" a simple app I made with PHP to Python in order to use it in Google App Engine.
In PHP I have the session check and the redirection with this code:
$fbuser = null;
$fb = null;
//Start Facebook
$fb = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true,));
$session = $fb->getSession();
if ($session) {
try {
$fbuser = $fb->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
if (!$fbuser) {
$loginUrl = $fb->getLoginUrl(array('canvas' => 1, 'fbconnect' => 0,
'req_perms' => 'publish_stream, offline_access, user_birthday, user_location, email',
'next' => $baseUrl,
'cancel_url' => $baseUrl ));
echo "<script type=\"text/javascript\">\ntop.location.href = \"$loginUrl\";\n</script>";
exit;
}
I've seen the Python-SDK for Facebook but it's very poor and hasn't all the useful method as PHP-SDK!
Can you help me translate this code to Python?
I think this example from the SDK git repository will give you a hint.