I'm trying to replicate the same functionality as someone else has already achieved on this page here:
http://www.facebook.com/PowerPhotoUploader?sk=app_152884604799537
am not bothered about the forced like fangate part, can do that no probs.
I need to achieve this without requesting any user perms the same way they have
I've got close, but not quite right yet.
Have created a test album on this page: http://www.facebook.com/pages/Demo-Album-Upload/227979503931257?sk=app_153866511376478
The code I have is uploading my specified image, however it is ignoring the album id I input and instead, uploading to an album on my own profile.
Code so far is:
<?php
$app_id = "XXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXX";
require 'facebook.php';
$facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, 'cookie' => true, 'fileUpload' => true,));
$signed_request = $facebook->getSignedRequest();
//print_r ($signed_request);
$page_id = $signed_request["page"]["id"];
//Upload To Page Album
$facebook->setFileUploadSupport(true);
$album_id ='59125';
$file_path ='image2.gif';
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($file_path);
$data = $facebook->api('/'.$album_id.'/photos', 'post', $args);
print_r($data);
?>
Have already read through a lot of forum material, have set the filuploadsupport, set file upload to true, but most of the info I can find so far reuires perms & access token, however sample above has managed to achieve with neither - any thoughts?
Regards Tony
Tony the trick here is that you only need one access token, not a new one for each user. The idea is that the user is not posting to the page, you are posting to the page, so you do not need an access token from the user. However since this is a secure call to Facebook you still need to provide an access token that has the ability to publish to the page.
The simplest route to get an access token that you can use would be to manually give the application the manage_pages and offline_access permissions for your account. Then just grab the access token for for your account and use it for all calls.
Related
I'm trying to retrieve my albums and photos from facebook using the Facebook api for php. I've created an app now i have my AppId and my AppSecretId. I don't need any login window because I want to display my own photos, not the ones from any other user, but everytime I try to retrieve them, I receive this message "An active access token must be used to query information about the current user."
I know that if I were going to show the photos from another users, then I should ask for their login and then generate the access token, but this is not the case.
I have tried generating an App token and tried to use this as an access token but it's not working either.
This is the code I'm using:
require_once("facebook/facebook.php");
try{
$facebook = new Facebook(array(
'appId' => '<APPID>',
'secret' => '<SECRETID>',
'cookie' => true
));
echo($facebook->getUser());
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
$me = $facebook->api('/me');
$albums = $facebook->api('/me/albums', array('access_token' => $facebook->getAccessToken()));
echo($albums);
}catch(Exception $e){
echo "THIS IS THE ERROR: " . $e->getMessage();
die;
}
I hope you can help me with this. Thank you so much!!
The easiest way to generate a Facebook access token for your personal account is to visit their Graph API Explorer. It will automatically generate an access token that you can copy/paste into your code.
https://developers.facebook.com/tools/explorer
Additionally, you can use the 'Get Access Token' button on that page to request an access token with specific permissions.
I am working on a site and i need to post page links from that site to a special user wall or page if something is published on it which means i only need one user to post that question.
the problem which i am facing is access token since i am not trying to show facebook login page in front of website traffic. its not like sharing on user wall we are basically trying to post status on a page automatically so any help for this problem will be appreciated.
i am using the following code which seems to work fine as long as the access token for the page is valid but i need to make something permanent so that i can add all the info in the php code hardcoded and it work for some weeks at-least without changing the api key or token
$appid = 'code';
$appsecret = 'code';
$wall= $pageId = 'code';
$token='page token';
$facebook = new Facebook(array('appId' => $appid, 'secret' => $appsecret, 'cookie' => true));
$params=array( 'message' => $msg,'name' => $title, 'caption' => $title, 'link' =>$uri,'description' => $desc, 'picture' => $pic, 'access_token' => $token,'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) );
$result = $facebook->api($wall.'/feed/','post',$params);
if i make token from the graph api debug page then it works fine but after some time i got the message "OAuthException: An active access token must be used to query information about the current user."
i am really desperate to find something solid for it.
thank you
You can write a backend script (maybe a cron job using the FB PHP-SDK) that runs as the special user and makes the desired FB api calls. For this, you will want to use a long-lived access token, which needs to be renewed every 60 days. You might also try your FB api calls using an App Access Token, which do not expire but also do not support all FB publishing and other operations.
To post to someones wall (or perform any action with the open graph) you must be authenticated with facebook as somebody in their graph or an application that they have granted permission to.
You should look at the permissions that can be set using the oauth scope attribute...
https://developers.facebook.com/docs/reference/dialogs/oauth/
To me it looks like they will need to grant your application publish_stream if you want to write to their friends walls as well as the user that you are authenticated as.
https://developers.facebook.com/docs/reference/login/extended-permissions/
I have just started with the Facebook application development. I downloaded the PHP SDK from Facebook couple of weeks ago.
Following is the first piece of code which i wrote.
<?php
require 'src/facebook.php';
$app_id = 'My APP ID';
$application_secret = 'My APP Secret';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $application_secret,
'cookie' => true, // enable optional cookie support
));
$uid = $facebook->getUser();
echo $uid;
?>
But it is always giving me 0 as a result even when i am already login into my facebook account in other tab.
My Canvas URL is pointing to my localhost as i have hosted this app on my local machine only.
I have read almost every post related to this issue but i was not able to solve this issue somehow.. It would be really helpful if anybody could help me in resolving this issue.
Cheers.
Ajay
The code will always return 0 unless you have authorised the app - even if you are logged into facebook elsewhere. Login to your app by going to the URL outputted by $facebook->getLoginUrl();
Then, you will find that $facebook->getUser() will return the correct User ID.
I've been working on a way to determine if a user likes a particular page so a tab on that page can be fangated or not. I didn't want to prompt the user for authorization for user_likes, so I avoided the JS SDK and used the PHP SDK:
<?php
require 'src/facebook.php';
$app_id = "...";
$app_secret = "...";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
/* testing response */
if ($like_status) {
/* liked content */
} else {
/* not liked content */
}
?>
My problem is signed_request is passed only when the code is on the FB tab--if I hit the PHP page outside of FB, I get nothing. I wondered if there's a way to get this user info outside of Facebook.com.
You can try saving the signed_request in a session or cookie so you can use it until it expires. After which, the user will have to come through the tab again to renew it. Naturally, if you are planning to use cookies, you should keep the signed_request decoded so no one can find and use the access_token from the cookie.
Sounds like this isn't really feasible outside of the Facebook tab. We've begun deploying our tabs in PHP in order to pull this data without an action on the user's part.
Thanks!
I have been working on the Graph API for the past few days. When I try to make an API call using the PHP-SDK, i get an empty json object as a response. But when i copy the access token from the links that are provided by FB in http://developers.facebook.com/docs/reference/api/, i get a json response.
I assume that this is because of the Access Token that i am sending in the request.
Help Appreciated.
My Code :
include_once "fbInit.php";
$access = $facebook->getAccessToken();
$user = $facebook->getUser();
echo $access;
if(!$user)
{
$loginUrl = $facebook->getLoginUrl();
header("Location:".$loginUrl);
}
$userWall = $facebook->api('/me/feed',array('access_token'=>$access)) ;
var_dump($userWall);
getAccessToken() only returns the internally stored access token in the SDK, it doesn't generate or fetch one. One advantage of using the SDK is that you don't need to pass in the access token directly like you're doing toward the end of your example. Once the user clicks the link generated by getLoginUrl(), the access token will automatically be stored in the SDK and the api method will work. Posting to a user's wall requires more parameters than you're passing in, however. You can see a complete example of how to do this in the recently-released PHP SDK docs at https://developers.intern.facebook.com/docs/reference/php/facebook-api/
This works for me:
index.php
<?php
require_once("./config.php");
$me = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$user = $facebook->getUser();
$userWall = $facebook->api('/me/feed',array('access_token'=>$access_token));
var_dump($userWall);
?>
config.php
<?php
require_once("facebook.php");
$app_id = "app_id";
$app_secret = "app_secret";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
?>
The api suppose to fetch the access token for you - it form a access_token graph api at base_facebook.php line 664. Unfortunately this api call maybe failed due to your redirect uri - some had mentioned that you should not have a "?" in your redirect uri and in my case - there is a tail "/", which caused this call failed. And same redirect uri had no problem if you direct call through "https://graph.facebook.com/oauth/access_token?".
Try to put a log to print out the response result from facebook in _graph() function within base_facebook.php will help you quickly determine whether you are hit by same problem as me.