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...
Related
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
I am trying to write a php script that can upload a daily photo to my personal facebook wall. I need this to be done at anytime and not just when I am logged in. I am wondering if it is possible to upload a photo to my wall using php only or do I need to use an app with the right permissions. If so do I have to personally install that app and give correct permissions for my page to make this possible, if it is possible, and can this be done automatically?
What I want to do:
In short I want a users twitter feed to be converted and put on an image. I have the script that automatically does this already. However I would love it if after the twitter user tweets then this calls my script and converts it to an image (again I have done this code already) but...
Now I need a script that can automatically upload this to my personal wall after the image is created. Is this possible?
Well, you can use the extended user access token to get it working for a maximum of 60 days. After that, the token will expire and then you'll have to refresh the token and that is not possible without the user's interaction.
The documentation states the same:
Even the long-lived access token will eventually expire. At any point, you can generate a new long-lived token by sending the person back to the login flow used by your web app - note that the person will not actually need to login again, they have already authorized your app, so they will immediately redirect back to your app from the login flow with a refreshed token - how this appears to the person will vary based on the type of login flow that you are using, for example if you are using the JavaScript SDK, this will take place in the background, if you are using a server-side flow, the browser will quickly redirect to the Login Dialog and then automatically and immediately back to your app again.
After doing the above you will obtain a new short-lived token and then you need to perform the same exchange for a long-lived token as above.
To get the extended token make the following call from your server:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
Code
$app_id = APP_ID;
$app_secret = APP_SECRET;
$post_login_url = APP_URL;
$album_name = 'test';
$album_description = 'desc';
$code = $_REQUEST["code"];
//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 {
// access token
$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 = $params['access_token']; // <---
// extended token
$extended_token_url= "https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=".$app_id."&client_secret=".$app_secret."&fb_exchange_token=".$access_token;
$response = file_get_contents($extended_token_url);
$params = null;
parse_str($response, $params);
$extended_access_token = $params['access_token']; // <---
// Upload to the photos to the album here
}
That's the complete auth process, incl the extended token. You can find many posts regarding the upload of the photo, simply use the $extended_access_token with your calls.
(In case you are not aware, Graph API Explorer is a fantastic tool of facebook to test the APIs).
Good luck!
I am trying to obtain a user access token from Facebook with the PHP-SDK. The server-side log-in process works great, redirecting to the correct page and providing $code too.
After the redirect (and checking whether $code exists) I then call the following API method to exchange $code for a user token (API parameters are suppressed here):
$fb->api('/oauth/access_token',array(
'client_id' => APP_ID,
'client_secret' => APP_SECRET,
'redirect_uri' => REDIRECT_URI,
'code' => $code
));
$fb_token = $fb->getAccessToken();
echo $fb_token;
Unfortunately, all I get from this script is still the app token. In fact, reducing the script to just the following does not make any difference:
$fb_token_user = $fb->getAccessToken();
echo $fb_token_user;
So I assume it is all about the /oauth/access_token call which for some reason is not working or at least does no action. It does not return any errors either.
I have tried and tested several alternatives, such as the get_file_contents() method suggested in the Facebook tutorial as well as adding additional parameters to the above API call (e.g. type=client_cred and more), but never succeeding.
Does anybody see why the API call is not working? Is it the script or could it be some app setting on Facebook?
Thanks in advance!
Solved! The code in the question is still not working in spite of ever so many threads on this and other communities stating it does (oddly enough it seemed to work for them).
So I figured I had to go back to the file_get_contents() method as explained by Facebook and it turns out this method was previously not working because allow_url_fopen is disabled on my server. Not wanting to undergo the risk of enabling it, I discovered cURL as a work-around.
And here is the working code:
// cURL Load Function
function cURLget ($ch_url) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$ch_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
$ch_send = curl_exec($ch);
curl_close($ch);
return $ch_send;
};
// Obtain User Token
$fb_token_get = cURLget("https://graph.facebook.com/oauth/access_token"
. "?client_id=" . APP_ID
. "&client_secret=". APP_SECRET
. "&redirect_uri=" . urlencode(REDIRECT_URI)
. "&code=" . $CODE
);
$fb_token_params = null;
parse_str($fb_token_get,$fb_token_params);
// Print Token Data
echo "Token: " . $fb_token_params['access_token']
. "<br />Expires: " . $fb_token_params['expires'];
Thanks to #FabioAntunes for trying to help me with the original code and to #ShawnECarter for his script for the cURL Load Function.
Stumbled upon this problem today too. Seems my PHP SDK (v.3.2.2) offers a function that solves the problem: setExtendedAccessToken(). (Does pretty much the same thing: Calls graph's "/oauth/access_token" and exchanges the old token against a "long-lived" one).
So I am encountering some weird problems with the Facebook SDK. I use both the JS SDK as the PHP SDK. To log a user out I use the following onclick:
Log out
After I clicked that link, when I try to perform any JS SDK call, like: FB.getAuthResponse (); it returns, as expected, null.
But after clicking the link, when I use the PHP SDK to check if someone is logged in, or just use the following SDK function: $this -> facebook -> getAccessToken (); it returns a valid token as if I am still logged in.
What am I missing here?
By the way, do you need some of my PHP code where I check for login?
Thanks in advance!
The PHP sdk saves the access token into local session. That's a huge problem. Especially in times when the facebook token expires and locally the SDK thinks it's still active. The solution we use is everytime we need user data we check against the access token to see if it's still valid. People would think this should be a mndatory behaviour of the SDK (to guarantee an active token) but sadly that's not the case.
This is snippet of the code we use.
$graph_url = "https://graph.facebook.com/me?access_token=". $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
//Check for errors
if ($decoded_response->error) {
// check to see if this is an oAuth error:
if ($decoded_response->error->type== "OAuthException") {
// Retrieving a valid access token.
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . FB_APP_ID
. "&redirect_uri=" . urlencode($redirect_url)
. "&scope=" . implode(',', $permissions);
exit("<script> top.location.href='" . $dialog_url ."'</script>");
}
else {
//handle other error types
}
else {
return $facebook-getUser();
}
This is part of our own getUser function. Hope it will help you to implement your own :)
It would seem that this would be a simple thing to do, but I just can't seem to find anything on how to accomplish this on the Facebook Developer forum, the API nor Google.
I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
According to the Facebook API documentation, notifications are generated automatically - so I have no control over them, and I can't seem to get the Bookmark counter to update automatically either - I have to problem getting it to increment when the user visits the app canvas page and something occurs, but what I want is to alert the user of a certain event without requiring the user to actually visit the app canvas.
I would guide you to the Request dialog page because it's (supposed to be) the right place to look. But as usual something is missing in the documentation.
Anyway, what you are looking for is a application-generated request:
<?php
$app_id = YOUR_APP_ID;
$app_secret = YOUR_APP_SECRET;
$token_url = "https://graph.facebook.com/oauth/access_token?" .
"client_id=" . $app_id .
"&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$app_access_token = file_get_contents($token_url);
$user_id = THE_CURRENT_USER_ID;
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message=’INSERT_UT8_STRING_MSG’" .
"&data='INSERT_STRING_DATA'&" .
$app_access_token . "&method=post";
$result = file_get_contents($apprequest_url);
echo("Request id number: ", $result);
?>
Described in a "blog post" instead of the documentation! and can be fount in this document (thanks #Charles).
I'm using the PHP SDK to write a simple app that looks for a certain change regarding the current user (not to seem mysterious, it's just that this part is irrelevant).
Actually, that's not entirely irrelevant, as the Facebook TOS prevents you from doing this in most situations, especially defriending, relationship statuses, etc.
As soon as this change is detected, I want to somehow alert the current user of the change, just not by posting it to the user's wall, this information needs to stay confidential - it needs to stay between the user and the app.
Request their e-mail and send it that way.