How to get data from facebook Ads on the Graph API? - php

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.

Related

Facebook Pages API Post Without User Login

I have created a FB App to publish posts as page from another site.
Many of the users will be publishing on my page, so I'm trying to create a function without user login or session.
I'm now doing it via creating Page Access Token manually from Graph API Explorer and injecting it into the code.
If I could create the access token in the code without user login, it will be my solution.
Is it possible or anyone knows how to do it?
Any help is appreciated.
P.S: In case anyone wonder my code, it is below.
$config['appId'] = $appId;
$config['secret'] = $secret;
$fb = new \Facebook($config);
$message = array(
'access_token' => $page_access_token, //manually injected
);
$message['message'] = 'message';
$message['link'] = 'link';
$posturl = '/'.$page_id.'/feed';
$result = $fb->api($posturl,'POST',$message);
There is example code in the docs, this is how to post stuff with a specific Access Token:
$response = $fb->post('/{page-id}/feed', $data, '{access-token}');
Source: https://developers.facebook.com/docs/php/howto/example_post_links
Of course you can´t create a Page Token without user login, that would be weird. You can only create a Page Token with a User Token, and you can only get a User Token with login. Extended Page Tokens are valid forever, so you only need to create it once and just store it.
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Spotify Web API - Client Credentials - Accessing a users playlists

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.

Feed Offline Access

I have an app that when the user goes to the app, searches his feed for specific posts. (I have read_stream). But i needed the app to read their feed whenever I want, like once an hour. How can i read their feed without they are in the app ?
Every time the user, enters your app you must save is fb_id on your data_base and you must save the extended token. This token will be valid for 60 days, so everytime the user goes to your app you should renew it, this way won't expire.
I don't know if you're using PHP SDK but here's the way you can get the facebook extended token:
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
$uid = $facebook->getUser();
//now that you have the extended token and the user id save it on your database.
Now the next time you want to read the feed of the user offline, just grab the user id from the database and
//the next time you want to read the user feed, just grab the uid from your database and access token and set it
$facebook->setAccessToken($new_access_token);

facebook graph api user returns 0

I am trying to use Facebook integration with Graph API
I registered with FaceBook, got my appID and Secret, downloaded the facebook-php-sdk-master and used this code:
require_once("facebook-php-sdk-master/src/facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
echo "user = ".$user;
Now when I try to use this code it always prints user = 0.
Have i Missed anything ?
ps: I have authorized the app too.
PPS: Could this because I am trying to test my code from localhost and facebook needs a website name for this to run ?
Thanks
You need to make sure that you have authorized the application. Just because you created the app does not mean the app knows who you are or can access your data.
Here is more info about logging in: https://developers.facebook.com/docs/technical-guides/login/
You first need to login and authorize the app.

access graph data without forcing user to login

I am looking to pull data from a fb page to a website without having a user authenticate.
The data I wish to display on a webpage is the event listing for a fb page, both the page and the events are open, and I can grab the page and event data ok, eg
http://graph.facebook.com/PAGEID
http://graph.facebook.com/EVENTID
But to get a list of the events in order to get the eventIDs an access token is required eg
http://graph.facebook.com/PAGEID/events
Is this possible to get this data without having a user login to fb and authorise with an application ?
I have looked at just saving an access token but this seems to expire.
any suggestions/pointers would be great, thanks in advance
Providing a quick solution to this set up an app and get an access token, note it would be best to cache the result data so as to not make an api call on each page load
<?php
//set your app id, client_secret and fb id
$client_id = 'xxx';
$client_secret = 'xxx';
$fbID = 'xxx';
//get an access token
$access_token = file_get_contents("https://graph.facebook.com/oauth/access_token? type=client_cred&client_id=".$client_id."&client_secret=".$client_secret);
//use this access token to return the events
$jsonurl = "https://graph.facebook.com/$fbID/events?$access_token";
$json = file_get_contents($jsonurl,0,null,null);
// do stuff with $json
?>

Categories