I'm trying to validate Facebook App ID and App Secret. With Graph Explorer, I'm able to use access token in shape of {APP_ID}|{APP_SECRET} to get app details on /v2.5/{APP_ID} endpoint.
It doesn't work with PHP SDK though. The error I'm getting is:
Unknown path components: /{APP_ID}
The code I used is this:
$facebook = new Facebook\Facebook( [
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => '2.5',
] );
$facebook->get( '/' . $app_id, $app_id . '|' . $app_secret );
It does the validation, because with invalid app ID I'm getting:
Error validating application. Cannot get application info due to a system error.
And with invalid secret:
Invalid OAuth access token signature.
How can I get the app details just like in graph explorer? I have no idea what I'm doing wrong, the request seems to be correct. Please help.
There is a typo in default_graph_version. It needs to be prefixed with v (standing for version), so the correct declaration is:
'default_graph_version' => 'v2.5',
Related
I'm using PHP Graph SDK to simplify logins to my site using Facebook.
The problem I'm getting is the error message after successfully logging into Facebook: "The redirect_uri URL must be absolute".
I've trawled the web (didn't find a solution) and added the redirect URL as "Valid OAuth redirect URI" at Facebook Dev Apps.enter image description here
But the error persists.
Here is the code:
$fb = new Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => FACEBOOK_APP_SECRET,
'default_graph_version' => 'v2.3',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email', 'user_location'];
$loginUrl = $helper->getLoginUrl('http://www.example.com/login_facebook.php', $permissions);
The full URL generated by the SDK is:
https://www.facebook.com/v2.3/dialog/oauth?client_id=9999999999999999&state=9999999999999999999999b&response_type=code&sdk=php-sdk-5.5.0&redirect_uri=http%3A%2F%2Fwww.example.com%2Flogin_facebook.php&scope=email
Your help is much appreciated. Thanks!
Just that, I'm reading the documentation and it's seems like you can get access to the basic information without auth. But i'm getting an error
the code
$fb = new Facebook([
'app_id' => '...',
'app_secret' => '...',
'default_graph_version' => 'v2.2'
]);
$response = $fb->get('/1000785'); // a valid ramdom user_id
and the response
Type: Facebook\Exceptions\FacebookSDKException
Message: You must provide an access token.
File: ...src/Facebook/FacebookRequest.php
So, should I need to implement the "classic" login anyway?
Ok, just to clarify this issue. It's enough just generating an app access token.
How to generate Facebook App Access Token?
Simply, concatenating app_id and app_secret with a pipe symbol.
$appAccessToken = $config['app_id'] . '|' . $config['app_secret'];
$response = $fb->get('/0123456789', $appAccessToken);
Cheers!
i am using facebook PHP sdk (v4) to fetch user information,
after installing SDK, i add the code
$fb = new Facebook\Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'v2.5',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$access_token= //copied from my https://developers.facebook.com/tools/explorer/
$response = $fb->get('/me?fields=id,name', '$access_token');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
but when i run the page it gives me this error,
Graph returned an error: Invalid appsecret_proof provided in the API argument
i am copying the app secret correctly, what could be the reason for such error and how should i fix this ?
You may want to read this: https://developers.facebook.com/docs/graph-api/securing-requests
appsecret_proof is a separate parameter that is generated by using the App Secret, but it is NOT the App Secret. Information about how to generate it can be found in the docs.
This might help someone who lands here like me. This error also happens if the App Secret is wrong for the particular App ID
Late reply but in my case was something else, so I hope it helps someone else. I see you commented:
//copied from my https://developers.facebook.com/tools/explorer/
On the top right corner of the explorer tool make sure that the selected Application is the same with the one you are using for:
$fb = new Facebook\Facebook([
'app_id' => 'my app id',
'app_secret' => 'my app secret',
'default_graph_version' => 'vX.Y',
]);
If you have another Application selected and use an Access Token given for a different one (basically the default Explorer App) the hash creating the appsecret_proof will be incorrect and you will always see this error.
You need to select your created APP in the Facebook Graph Explorer. I was selecting my username. When I selected the app and click on "Get token" it shows me de "login panel" from my app and can generate the token.
It works for me.
Or the issue could be you haven't enabled API calls to your Fb App. To enable the Api call :
1. Go to your apps section
2. Select the app that you want to use for the intergration
3. Under the App setting select advanced
4. Under security, ensure that the Allow API Access to App Settings is enebale
to yes
5. If need be enable any other permission that might be required.
Hope that solves the issue
I am trying to read the posts of a facebook page, using the actual Facebook SDK 5 for PHP. I have created a facebook app, downloaded and "required" the SDK successfully, but I cannot get the request to run, since it always throws a Invalid OAuth access token signature exception.
The code I have so far:
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $secret,
'default_graph_version' => 'v2.5'
]);
$accesstoken = file_get_contents("https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $appid
. "&client_secret=" . $secret
. "&grant_type=client_credentials" );
//following lines are something I just tried, there have been multiple
//Variantions, none ever worked
$cilent = $fb->getOAuth2Client();
$accessToken = $cilent->getLongLivedAccessToken($accesstoken);
All in all my main issues are:
how to get a valid access token? Since I just want to read public data (feeds of a page), I do not need some special permissons and I am quite confused which on Type of access token I need.
Are there any settings within the facebook app I need to make, or make sure, to get this up and running?
I have googled the web for examples, but I could find examples either using older SDKs (if used at all), or showing how to post data.
I do not have too much experience with that topic and I would appreciate some explanation, or a piece of example.
[UPDATE]
I could manage to get the feeds, some dive through the source was needed.
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.5'
]);
$req = $fb->get(
'/<page-id>/feed?fields=full_picture,message,name,picture,story,posts&limit=20',
$fb->getApp()->getAccessToken());
In case of success everything one is looking for is included in $res. For sake of simplicity I removed the error handling.
Because everything is desinged to read public data, there is no need for any access-token huzzle, however, retrieving user Data, posting or something else, is not possible with this solution.
Cheers!
I'm writing a Facebook login integration using the PHP SDK and running into the above error.
On our main page we have the following code to request public_profile and email.
$fb = new Facebook\Facebook([
'app_id' => '{our_app_id}',
'app_secret' => '{our_app_secret}',
'default_graph_version' => 'v2.4']);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email'];
$loginUrl = $helper->getLoginUrl('http://oursiteurl/includes/fbcallback.php/',
$permissions);
This code pops open a new window, processes the Facebook login, authorizes the app permissions, and sends us back to fbcallback.php. Users can disallow revealing their email to our app, but our app absolutely must have being email. So fbcallback.php evaluates whether or not we received the email. If it did we enter a new record in our database; if not, I want it to present an option to rerequest the email permissions. For this I'm trying the following:
$fb = new Facebook\Facebook([
'app_id' => '{our_app_id}',
'app_secret' => '{our_app_secret}',
'default_graph_version' => 'v2.4']);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email'];
$accessToken = $helper->getAccessToken();
$redirect = "http://media.pvplive.com/includes/fbcallback.php/";
$loginUrl = $helper->getLoginUrl($redirect, $permissions, TRUE);
Basically, I'm looping back to fbcallback.php, but this time with the rerequest flag set to TRUE. I receive no complaints on landing on fbcallback.php the first time, but when I click on the link for the new $loginUrl it throws the following error:
Length of param app_id must be less than or equal to 32
I haven't been able to find any answers explaining this error yet or what needs to be corrected to get rid of it. I'm using the answer provided here for code for rerequesting permissions already denied using the PHP SDK.