I´m building a facebook app with php,
everything works perfect, I do successful dialog auth
I have the short_live token
I generate the long_live_token and save it to some directory
what I want to do is that in canvas app the user selects some stuff and activates a mechanism that regularly posts stuff, this is why I save the token.
but what can I do with it?!
I find a lot about generating the access_token but nothing about how to use it!?
Where can I add it as parameter? What is the key?
example:
I´m using facebook sdk for php for post sth. to a wall like
$msg_body = array(
'message' => "wassup yo"
);
$facebook->api($uri, 'post', $msg_body );
but this only works if
$facebook->getUser();
is returning a user
how can I use my stored access_token to do the same?
I believe there is a function called "setAccessToken" in the Facebook PHP SDK. You would just need to set it with that function and it gets added to every call automatically.
Manual way:
$params = array(
'message' => 'wassup yo',
'access_token' => '[your-token]'
);
$facebook->api($uri, 'post', $params);
You could also do this with CURL, this would be an example URL;
$url = 'https://graph.facebook.com/' . $userId .
'/feed' .
'&access_token=' . $accessToken .
'&message=' . $userMessage;
Basically you just add the Access Token as a parameter like the message.
Just make sure you are using secure calls, see this article for an example of using CURL with the Facebook API and usage of "appsecrect_proof": http://www.devils-heaven.com/extended-page-access-tokens-curl/
IMPORTANT: Be sure that the message parameter is always 100% user generated without any prefilling (see Platform Policy) and keep in mind that you need to go through a review process with pulish_actions to make it available for other Users: https://developers.facebook.com/docs/apps/changelog
Related
I've created and stored permanent facebook page access tokens. Must I still use a user access token each time I want to post to a fb page feed, or can I use only the page access token?
Here is command I'm using to setup the post now:
$page_post = (new FacebookRequest( **$session**, 'POST', '/'. $page_id .'/feed', array(
'access_token' => $access_token,
'name' => 'Randy Steel',
'link' => 'http://www.LIPSapp.com/',
'caption' => 'Example text',
'message' => 'This is my link!',
) ));
Where $session holds the user access token and $access_token holds the permanent page access token.
If, because I have the perm page access token, I can skip getting/updating the
user access token (60 days at best), how would the above command change?
What would the value of $session need to be, or can that parameter be omitted?
Thanks in advance for any help.
With Facebook's new update like 2 years ago, pages acts like normal profile pages. So you only need access tokens of page not administrator's. Access tokens can be changed overtime or can be expired so it's best you to store administrators access token just in cas the access key losts its validity.
If you always want to post "as Page", you can use the stored Page Token - in fact, you HAVE to use the Page Token to post "as Page". You only need the User Token once, for getting an Extended Page Token.
I would not even use the PHP SDK for that, basic CURL calls are good enough. A small example of using CURL with the Facebook API can be found here: http://www.devils-heaven.com/extended-page-access-tokens-curl/
im building a facebook app and i want to notify the user
https://developers.facebook.com/docs/games/notifications
im using facebook php sdk
what i do:
user auths the app and accepts permission
i get the accesstoken like:
$facebook->getAccessToken()
and then i generate a long-time token like:
public function generateLongTimeToken($token){
$long_time_token_req_body = array(
"grant_type"=>"fb_exchange_token",
"client_id"=>$this->facebookOptions["appId"],
"client_secret"=>$this->facebookOptions["secret"],
"fb_exchange_token"=>$token
);
$query = http_build_query($long_time_token_req_body);
$lttreq = file_get_contents("https://graph.facebook.com/oauth/access_token?".$query);
$lttresp = parse_str($lttreq, $output);
if ( array_key_exists("access_token", $output)){
$this->logger->info("Facebook-app: Successfuly generated long_time_token");
return $output["access_token"];
}else {
$this->logger->err("Facebook-app: generating oauth long_time_token failed \n".$lttreq);
return false;
}
}
some later i use this token for background processes to post on the users wall and them all work fine
now i also want to notificate the user like that :
public function notifyUser($message,$facebookClientId,$token){
$appsecret_proof= hash_hmac('sha256', $token, $this->facebookOptions["secret"]);
$req_body = array(
"access_token"=>$token,
"appsecret_proof"=>$appsecret_proof,
"href"=>"/index",
"template"=>$message,
"ref"=>"post"
);
$query = http_build_query($req_body);
$url = "https://graph.facebook.com/".$facebookClientId."/notifications?".$query;
$lttreq = file_get_contents($url);
return $lttreq;
}
but when i try to notify the user i always get empty data back
when i open the url in browser with all parameters facebook returns the same
{
data: [ ]
}
so i have no idea whats going on,when i look on SO i only find about people posting to sites but i want to notify the user itself
thanks for any help
First, from the Facebook docs:
Currently, only apps on Facebook.com can use App Notifications.
Notifications are only surfaced on the desktop version of
Facebook.com.
Also, an App Token is needed, not a User Token.
Btw, file_get_contents is very bad, use CURL for Facebook. May be another reason why it does not work. A basic example of using CURL with the Facebook API: http://www.devils-heaven.com/extended-page-access-tokens-curl/
Additional Info: I recently wrote a blogpost about App Notifications, it is in german but the small code part may be interesting for you: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/
I am trying to get facebook php sdk v4 to work with my app. I am using Drupal and an nginx server with phpfpm.
When I try to get the session from facebook sdk, I get the error - Missing redirect uri parameter. While debugging, I found 2 strange things are happening -
1) If I copy and paste the get url(generated by the sdk) in my browser, a valid access token is returned.
2) If I change the method for retrieving the access token from GET(default) to POST in the sdk, a valid access token is returned to my app.
Also, when using the access token I received from POST call, if I make further GET requests to the graph api, none of them work.
However, if I make a POST request, such as posting to my wall, it works.
Also it should be noted that the sdk is internally using curl to make all requests.
Edit :
Code Example -
To generate login url
$helper = new \Facebook\FacebookRedirectLoginHelper($this->redirectUrl);
$loginUrl = $helper->getLoginUrl(array('public_profile', 'email', 'user_friends', 'publish_actions'));
Now the person is taken to facebook for authentication and redirected to my redirect url. Here the code is -
$helper = new \Facebook\FacebookRedirectLoginHelper($this->redirectUrl);
$session = helper->getSessionFromRedirect();
Since getSessionFromRedirect internally uses a GET call, it doesn't work for me. When I change the sdk code to use POST, a valid access token is returned.
Also when I get a valid session using a POST request, this works
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
But this doesn't work -
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Name: " . $user_profile->getName();
Clearly there seems to be an issue with all the GET requests. Any hints on what can be the problem?
Solved the problem finally. Turns out drupal is changing the & in url for GET requests to & .
Related to this .
I am trying to use open graph to read story information off a public facebook page (http://www.facebook.com/onlinehorsesupplies). I want to obtain the message content of the latest post for use elsewhere on the related ecommerce website. I could scrape it but using open graph seems like it should be a better way to go.
Using the http://developers.facebook.com/tools/explorer I found the URL I need to use quite easily but I had to click on the 'get access token' to make it work. I use this URL:
https://graph.facebook.com/188288097875046?fields=posts&access_token=
If I use the access token copied off the explorer page it works fine. If I use an access token off my own app it fails with "Bad request...". How can I get an appropriate access token in my php code?
I think your app needs to request a token first. At least thats how it worked for me.
Something like:
$app_token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$response = file_get_contents( $app_token_url );
parse_str( $response, $fbinfo );
then you can use that token for the request...
I have to post on a users wall when he is offline. I went through number of posting and checked the code written below seems ok and also not throwing error and complete successful yet I am unable to see posting on users wall. Pl help.
$facebook = new Facebook(
array(
'appId' => "446262598768110",
'secret' => "<REMOVED>",
)
);
$facebook->setAccessToken("<REMOVED>");
//create message with token gained before
echo " lets try posting";
$access_token = $facebook->getAccessToken();
$user_id = $facebook->getUser();
echo "\ndone3..... $access_token.-----.$user_id.----new\n";
$post = array(
'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
//and make the request
$res = $facebook->api("/me/feed", 'GET', $post); //have tried both Post and Get method but result remain same
offline access was removed quite a few months ago. also, you do have to "POST" to facebook graph, everytime you want to publish something.
without seeing the result of the facebook graph, it's kind of impossible to help you at all.
nevertheless: double check, that you're using the correct access token. you have to extend the short lived access tokens by your own (https://developers.facebook.com/docs/howtos/login/extending-tokens/), so that the access token itself is still valid when you want to post by cron-job.
if you're trying to post by using the app access token, you can't use "/me/feed" at all.