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
Related
I'm trying to access users playlist tracks by using the client credentials flow.
Spotify getting playlist documentation: https://developer.spotify.com/web-api/get-playlists-tracks/
Spotify getting client credentials documentation: https://developer.spotify.com/web-api/authorization-guide/
First question, is it possible to get a users playlist tracks using client credentials flow? I'm using this flow since I'm unable to pop up a login box for the user to login.
Secondly, I've tried using https://github.com/jwilsson/spotify-web-api-php client credentials flow (Docs: http://jwilsson.github.io/spotify-web-api-php/authorization.html) by practically copying the code at the bottom of that page:
<?php
include('vendor/autoload.php');
$session = new SpotifyWebAPI\Session('Tobys client id', 'Tobys secret', 'http://localhost/callback');
// Request a access token with optional scopes
$scopes = array(
'playlist-read-private',
'user-read-private'
);
$session->requestCredentialsToken($scopes);
$accessToken = $session->getAccessToken(); // We're good to go!
// Set the code on the API wrapper
$api->setAccessToken($accessToken);
$playlists = $api->getUserPlaylists('USER_ID', array(
'limit' => 5
));
foreach ($playlists->items as $playlist) {
echo '' . $playlist->name . ' <br>';
}
This gives me Notice: Undefined variable: api in /var/www/html/dev/testing.php on line 16
I've also tried creating the API variable using $api = new SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/ tokens.
Thanks.
First question, is it possible to get a users playlist tracks using
client credentials flow?
Yes, retrieving tracks for a playlist doesn't require user authentication as part of the access token.
I've also tried creating the API variable using $api = new
SpotifyWebAPI\SpotifyWebAPI(); but this says I need user information/
tokens.
Looking at the code (Session class, SpotifyWebapi class), it does look like you should set this up by doing
$api = new SpotifyWebAPI\SpotifyWebAPI();
$session = new SpotifyWebAPI\Session($clientId, $clientSecret, $redirectUri);
$api->setAccessToken($session->getAccessToken());
When that's set up you should be good to use the getUserPlaylists method like you're doing in your example code.
i'm trying to write a script to post to a page while the admin is offline.
my application has the manage_pages extended permission of the admin user.
here is my code:
require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID', // YOUR APP ID
'secret' => 'MY_SECRET', // YOUR API SECRET
'cookie' => true
));
$user_admin_id = 'MY_ADMIN_ID';
$page_id = 'MY_PAGE_ID';
//get the access token to post to my page via the graph api
$accounts = $facebook->api("/" . $user_admin_id . "/accounts");
foreach ($accounts['data'] as $account)
{
if ($account['id'] == $page_id)
{
//found the access token, now we can break out of the loop
$page_access_token = $account['access_token'];
break;
}
}
but I always get this message:
"Fatal error: Uncaught OAuthException: A user access token is required
to request this resource. thrown in
/home/itrade10/public_html/khodiersoftware/php-sdk/src/base_facebook.php
on line 1033"
You forgot to authorize the User, that´s how you get a User Access Token:
https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
Don´t forget to add the "manage_pages" permission in the scope Parameter. You will also have to use the Function "setExtendedAccessToken" of the PHP SDK to extend the User Token. After that, you will get a Page Access Token that is valid forever with the /me/accounts endpoint.
If you used getLoginUrl already, then there´s something wrong with that code, you may want to add it to the question.
Before getting the accounts (with /me/accounts, not with your id), get the User ID:
$user = $facebook->getUser();
If you got a valid User Token, your ID will be in the $user Variable.
maybe this one? https://developers.facebook.com/roadmap/offline-access-removal/ also see Getting long-lived access token with setExtendedAccessToken() returns short lived token
The error message is telling you exactly what the problem is... you need to get a user access token to access your page tokens
I am new to PHP and facebook API'S
I am trying to get data from my ads account on faceobook using PHP
I just want to know how much $ every ads spent
First of all - I did not understand if I must have facebook app in order to get data from my personal ad account ?
I assumed yes so I created one now..,
I am geting the acess token like this :
require_once("facebook.php");
$config = array();
$config['appId'] = 'myapp_id';
$config['secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
// Set a new app secret
$facebook->setApiSecret($config['secret']);
// If you do above, also set the app id
$facebook->setAppId( $config['appId']);
$access_token = $facebook->getAccessToken();
But when I am trying a get request like this:
https://graph.facebook.com/act_[my_account_number]/adgroups
I am getting a premisson exception that tell me
An access token is required to request his resource
But where do I put the access token ?
You need to add it at the end of the url as a url parameter:
"https://graph.facebook.com/act_[my_account_number]/adgroups" . "?access_token=" . $access_token
However, from the code I think you are getting an App Access Token, and you need a Page Access Token or a User Access Token to get the ads related to your Facebook Page.
Please refer to https://developers.facebook.com/docs/facebook-login/access-tokens/ for more information.
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;
}
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/