I am working on a Facebook application and I am having trouble sending notifications to users of my app. I reviewed the answers to similar questions on this site and I consulted the Facebook SDK page and tried to apply the following strategy:
<?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 $result;
?>
All I get is an error on my second call to file_get_contents. The error states:
failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden\r\n
Can anyone give me an idea of what's going on? My first request to graph.facebook.com worked just fine.
Check Facebook is doing OK: Does Facebook Platform Live Status have any relevant information?
Check you have no connection issues with facebook.com in HTTPS: Can you open the URL of the social graph for Facebook Platform's page in your browser?
Check again if the actual URL you're trying to access works and the URL is correct.
Come back here and update the question with the problematic URL and the exact method you're trying to access, or contact Facebook Platform support.
stolen from Facebook graph api doesnt response
Related
I'm following the steps very well described here https://stackoverflow.com/a/18399927/2510225 , but, from my server, I receive the following error:
{"error":{"message":"The access token does not belong to application APP-ID","type":"OAuthException","code":1}}
I can't figure what I'm doing wrong. Anyone knows if the process to get a permanent access token has changed, or is having the same issue?
The access token I'm using in the request is the user access token, which I think is correct.
In other words, I'm using this:
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}
With the app_id and app_secret of the app I want to publish on a page and the short-lived-token of the user that have created the app. Is that the right way?
Edition (Image to complement answer from #Sahil Mittal)
That's where I'm taking the API_ID (red arrow). That's correct, right?
Ok, That's how I've solved this, combining both solutions given [here][1] with some tries. :
1) Associate the app with the page (It was probably done)
http://facebook.com/add.php?api_key=_APP_ID&pages=1&page=_PAGE_ID
2)Take the CODE given here:
https://graph.facebook.com/oauth/authorize?client_id=_APP_ID_&scope=manage_pages&redirect_uri=http://www.facebook.com/connect/login_success.html
There will be a very fast output on the URL box of your browser, copy that fast. This output should be like this
https://www.facebook.com/connect/login_success.html?code=1234546bigstringwithlotsoflettersandnumbersdfdarsd#_=_
3)Use the CODE to take the short lived access token of the USER (I guess it can be the same get with the Graph API Explorer))
https://graph.facebook.com/oauth/access_token?client_id=_APP_ID_&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=_APP_SECRET_&code=_CODE_
4)Convert the short lived access token to a long lived access token (user yet):
https://graph.facebook.com/oauth/access_token?client_id=_APP_ID_&client_secret=_APP_SECRET_&grant_type=fb_exchange_token&fb_exchange_token=_SHORT_LIFE_ACCESS_TOKEN_
You can check if this access token is long lived in
https://developers.facebook.com/tools/debug/accesstoken
4) Go to Graph API Explorer (https://developers.facebook.com/tools/explorer), click on the "X" to clear the access token box, and fill in the long access token that you created in the previous step.
5) On the box bellow, choose /ACCOUNT/, to see all the pages of the user this access token is related with. The acess token of these pages are never expired access token, which can be verified in https://developers.facebook.com/tools/debug/accesstoken
That's how it worked for for me.
You forgot to replace APP-ID with the relevant App ID.
You can get the same from the app settings
To get the short-lived-token:
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $APP_ID
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,email";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
else
{
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $APP_ID
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $APP_SECRET
. "&code=" . $_REQUEST["code"];
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
}
I am trying to fix a fb login error. This code was setup before I started working on the site. Apparently it used to work. I have looked at the fb documentation and the code looks correct. Here is the call:
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id=' . $site->facebook_app_id . '&redirect_uri=' . urlencode('http://' . $site->domain . '/facebook.php?oauth_callback') . '&client_secret=' . $site->facebook_app_secret . '&code=' . get('code');
$token = file_get_contents($token_url);
The response is:
failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
' in .../facebook.php on line 21
The output of echo $token_url is:
https://graph.facebook.com/oauth/access_token?client_id=#####&redirect_uri=http%3A%2F%2Fdev.mypdxhomes.com%2Ffacebook.php%3Foauth_callback&client_secret= ####&code=AQClZMJ1NUYIwwXUABCw3g8hVhRFavrmQfCr5b-dS6PkVqA40WNfBhQTWHe7V7-q7HUDk2OTS50KGUHEa3TMYADWgkYp688rlSnGNyeetkFYWBuFrWOdeo_wBjdErBQ7n1iNHzetqMvlMtW9KK9TEmnIxmwXEdPl-T3CkRNbGp4g2oCSvXI_ozRo6LWDJMqg6nLq54ibag7k8muhjGZ97CYs
I have looked at a lot of posts about this issue, though none have helped me resolve my issues.
Found the solution to this here: Facebook Access Tokens - Server-Side Authentication
Ended up using the FB PHP SDK
im developing a new facebook application and need to send Appusers frind list application invite.
Example,
my friend X logged CityVille App,
my friend is offline but he send CityVille Invite.
i ask to X you send this invite ? he said no :) Invite getting from city ville Automatically. (No fb dialog)
How can i do this.
P.S:
My App Required permissions is:
email,offline_access,publish_stream,user_photos,read_friendlists
I tried following code on my friendlist, but i dont send invitation returing error
$app_id = $this->facebook_config['appId'];
$app_secret = $this->facebook_config['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 = 'USERID';
$apprequest_url ="https://graph.facebook.com/" .
$user_id .
"/apprequests?message='Mesaj'" .
"&data='bisi'&" .
$app_access_token . "&method=post";
$r = file_get_contents($apprequest_url);
echo $r;
error
{
"error": {
"type": "OAuthException",
"message": "(#200) All users in param ids must have accepted TOS"
}
}
As I understand you're asking about requests to use or invite users to the application rather than do something in the application. Well AFAIK the only way to do this is by emailing them. Facebook just don't allow this anymore, other that the default Facebook invite dialog.
The Facebook's API apprequests is to send request between the connected users only e.g. get this lost cow, or help me to win this battle ... etc.
Update: The proper way invite friends to connect is by using Facebook JavaScript SDK.
FB.ui({method: 'apprequests',
message: 'Plz add this, I need you to use this app desperately!',
to: 'any-friend-id'
});
I've just tested it and it allowed my brother to use it.
The error you are getting is most likely caused by your use of file_get_contents.
I would suggest using curl for this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apprequest_url);
$result = curl_exec($ch);
curl_close($ch);
Please be aware that (last time I checked) you must still prompt the user with some form of dialog before sending any app requests/invites. Doing it without a user's expressed permission is against FB's guidelines.
You can test the functionality of your request with the API explorer: http://developers.facebook.com/tools/explorer/?method=POST&path=me%2Fapprequests
Hi Im trying to write a facebook application that will post a photo to a users page. Ive pretty much word for word followed this tutorial.
but whenever I try and upload a photo I get this error:
{
"error": {
"type": "OAuthException",
"message": "A user access token is required to request this resource."
}
}
I beleive the problem is coming from this code as I'm sending the AppID, AppSecret and post_login_url (which are all correctly populated) however $response is completely blank:
$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'];
Any help would be appreciated, as always im on a tight deadline
Thanks,
Doug
The Application Access token is used to make API calls on behalf of an app, your underlying issue is likely that the API call you're trying to make has to be made on behalf of a User and needs a user access token produced when a user authorises your app.
The code you have above should be fine for getting an App Access token
I am working through the examples on the Facebook developer pages. I am using their example code.
As per the example code, when I try to perform
$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);
I get a HTTP 400 error. Now, I know my URL is fine, because manually going to that address works, and returns the access code as expected. I can then use this access code in the GRAPH API explorer to make posts without error.
I can get contents of other HTTPS resources, without any issue.
Googleing and searching the FB dev forums has gotten me nowhere, just people saying "your URL is wrong fix it" but I have no spaces or fancy characters anywhere in my URL so at a total loss as what to do here.
TIA.