i don't know why when i'm using facebook php sdk 3.1.1 on codeigniter with following code:
...
...
$this->config->load('facebook', TRUE);
$config = $this->config->item('facebook');
$this->load->library('facebook', $config);
echo $this->facebook->getAccessToken();
....
i retrieve app access token instead ( the correct ) user access token.
infact when i try to call
$this->facebook->api('/me')
php return me an error like this
Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in
/srv/www/...
i've try to change library version until 3.0, but nothing changed
Anyone know something about that?
/me returns your personal profile info. If you want to query /me, you need to authenticate via a user access token, which is above the app level access token.
Peruse at your leisure: https://developers.facebook.com/docs/authentication/
Related
When i use this code:
<?php
require_once "vendor/autoload.php";
$config = ...;
use FacebookAds\Api;
use FacebookAds\Object\Page;
Api::init(
$config['facebook']['app_id'], //APP_ID
$config['facebook']['app_secret'], //APP SECRET
$config['facebook']['app_access_token'] //Token generated by https://developers.facebook.com/tools/explorer for app
);
$page = new Page($config['facebook']['page_id']);
$leadgen_forms = $page->getLeadgenForms(); //heres an error
I get error:
Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: (#190) This method must be called with a Page Access Token in ...
But when I put page_access_token in place of app_access_token (from https://developers.facebook.com/tools/explorer) i get error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid appsecret_proof provided in the API argument in .... When i remove line:
Seems you are working on lead-gen forms that are meant for pages only. Your profile must have admin/developer role assigned. You definitely seem to have missed/copied incorrect value for one of the below.
Below details are copied from https://developers.facebook.com/docs/marketing-api/guides/lead-ads/retrieving for quicker understanding
One can read leads or real-time updates by:
Using a Page Access Token, i.e. the Page admin's access token for the page. Page access token also allows you to read ad specific fields
such as ad_id, campaign_id, etc., if you have atleast advertiser level
permissions on the ad account associated with the lead ad.
Using User Access Token belonging to the page admin. To access all of the lead data and the ad level data, the access token should have
manage_pages and ads_management scope.
You can manage user rights with Page roles. In addition, if you need
to allow leads download for user with non-admin role on the page, you
can whitelist it with leadgen_whitelisted_users endpoint.
The other answers are not showing how to actually send a Page Access Token instead of an App Access Token or User Access Token.
require_once "vendor/autoload.php";
$config = ...;
use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Session;
$api = Api::init(
$config['facebook']['app_id'], //APP_ID
$config['facebook']['app_secret'], //APP SECRET
$config['facebook']['app_access_token'] //Token generated by https://developers.facebook.com/tools/explorer for app
);
$page_api = $api->getCopyWithSession(new Session(
$config['facebook']['app_id'], //APP_ID
$config['facebook']['app_secret'], //APP SECRET
$page_access_token // <-- You can get this by accessing 'me/accounts' w/ the initial API
));
$page = new Page($config['facebook']['page_id'], null, $page_api); // <-- use the api with the Page Access Token here
$leadgen_forms = $page->getLeadgenForms(); //heres an error
I'm working on getting a fb auth working on my site. I have code in already to get the token and store it in a session variable. On a new page I have this:
echo $_SESSION['facebook_access_token']
That puts out a long auth token that I can paste into this site :
https://developers.facebook.com/tools/explorer/145634995501895/
I paste the string in to the top Access token box and can access the fields that I asked permissions for in an earlier step.
Back to the code, I pass this line:
$response = $fb->get('/me?fields=id,name','$_SESSION["facebook_access_token"]');
and get the following:
Invalid OAuth access token.' in /{web root}/facebook/src/Facebook/Exceptions
Now what the heck is going on here? How can I troubleshoot this?
I've searched for a Laravel Facebook SDK and I found this package that works with Laravel 4.2 (my laravel app version). I use this just to get my facebook page posts but I got into a problem, my access token expires every 2 hours or in some cases because of other reasons like facebook logout, etc.
I found out that there are 2 ways to handle expired access tokens, either to extend the access token, either to request a new one using the old access token.
The questions is how to request a new access token because I store it into my database and I need to get my facebook page posts all the time?
I've followed this instructions but for me it does not work.
When I use this code:
try
{
$token = Facebook::getTokenFromRedirect();
echo "Success<br>";
print_r($token);
}
catch (FacebookQueryBuilderException $e)
{
// Failed to obtain access token
echo 'Error:' . $e->getMessage();
}
the only thing what I see on the screen is Success and that is all, it's like the $token is empty.
I've also checked Facebook Query Builder which is included into SammyK LaravelFacebookSdk and tried to use getTokenFromCanvas() insted of getTokenFromCanvas() but I get this error: Method getTokenFromCanvas does not exist.
Any idea to make this work?
By default Facebook will return a user access token that expires after 2 hours. You can extend it for a long-lived access token that'll last 60 days.
Once you obtain an AccessToken entity in Laravel Facebook SDK 1.2, you can extend it like it shows in the example:
try {
$token = $token->extend();
} catch (SammyK\FacebookQueryBuilder\FacebookQueryBuilderException $e) {
dd($e->getPrevious()->getMessage());
}
In your GitHub issue you mentioned:
What I want to achieve is to get the posts from my facebook page
You can use a page access token to grab your page posts. You can obtain a page access token from the /me/accounts endpoint.
If you use a long-lived user access token to obtain the page access token, the page access token will never expire so it's ideal to store in your database to pull posts from a page.
See more about handling access tokens. Good luck! :)
On facebook documentation page, there is written:
Facebook::getAccessToken()
Get the current access token being used by the SDK. This may be a user access token or an application access token. See the permissions page for more information on access tokens.
But there is no explanation how to force the method to return app token, not the user token. It is not exmplained nor on permission page.
Cn anybody help, how this method works?
The App access_token is usually just APP_ID|APP_SECRET. In my code, I tend to just do this:
$app_token = APP_ID . '|' . APP_SECRET;
I've this error, but not always. (I use PHP SDK, latest version).
If I'm logged into facebook and i try to register and login, then the app say it's ok!
Also if i'm not logged into facebook, the app redirect me to login url, and here it's all ok.
But sometimes the app say this exception: OAuthException: An active access token must be used to query information about the current user. and the script redirect the user to loginUrl with a loop-redirect (because the access token isn't valid and user need always of loginUrl)
In the web some say that Facebook create a duplicate of Access Token and then access token of php sdk don't is = facebook.
For fix, the user must deletes cookies, how I can fix this?
Thanks a lot for reply, if code is need reply and I'll post it, have a good day! :)
Try to destroy the Facebook session, then redirect the user to login URI if you receive this error, this way you will get a fresh access token.
Notice: Access token expire after a very short, you can get a long living access token. The latest PHP SDK contains a public function called : setExtendedAccessToken()
you can call this function to automatically exchange the 2-3 hours living access token for a 60 days access token
Eg:
try {
$user = $this->facebooknew->api('/me');
$this->facebooknew->setExtendedAccessToken();
$access_token = $this->facebooknew->getAccessToken();
}
catch(FacebookApiException $e){
$this->facebooknew->destroySession();
header('Location: ' . $this->facebooknew->getLoginUrl($params));
//echo $e;
}