Get Facebook Marketing API Access Token Using Ads PHP SDK - php

I was wondering how does one get access to the FB Marketing API Access Token using the Ads SDK. Currently am I manually doing this:
https://www.facebook.com/dialog/oauth?client_id=<APP_ID>&redirect_uri=
<http://yourwebsite.com/>&scope=ads_management,manage_pages,publish_actions
Which returns a code like this:
?code=<your auth code>
Then I input into the next URL provided by FB:
https://graph.facebook.com/<API_VERSION>/oauth/access_token?client_id=<APP_ID>&
redirect_uri=<http://www.yoursite.com>&client_secret=<APP_SECRET>&code=<CODE>
This gives me the access token, but am assuming there is a better way to get the access token using the Ads PHP SDK provided? I hope. If anyone can help. Please share, thanks!

You can extend an access token to be a 'long-lived' server-side token. This extension will give it a lengthy expiration. More info here: https://developers.facebook.com/docs/facebook-login/access-tokens#long-via-code
You can also authenticate via the PHP SDK as you mentioned. Get started here: https://github.com/facebook/facebook-php-ads-sdk/blob/master/DEVELOPER_GUIDE.md#authentication-

Related

How to query Facebook's graph-api?

I am using PHP 5.3.8, therefore I can't use the latest Facebook SDK which works only with PHP 5.4.X.
given a url, I'd like my server to tell if this is a facebook-like-page, or not.
Do I have to use OAuth? I used to use the graph-api, but now I get all weird errors. like no access-token provided, no authorization etc.
My code was using cURL with GET and accessed https://graph.facebook.com/.urlencode("THE_PAGE_URL");
I don't know if this is not an overkill to do a whole authorization from my server, and if so, to whom? I must say I am lost in the facebook documentation, frankly.
I tried this sample code from Facebook, which does not work at all.
Is there a simple way to get info about a public like-page from facebook?
An easy way to make calls using the Facebook Graph API is with their Graph API Explorer.
You have to be logged into your Facebook developer account to access it obviously, but it is really helpful if you're experimenting with making different calls to a Facebook page.
Regarding oAuth, I would also highly recommend that you obtain a permanent page access token. It's the best way to mitigate the complexities of Facebook's oAuth process.
facebook: permanent Page Access Token?

Is there a way on how to get an access token for User Data Permission in PHP?

I am using PHP to work on Facebook, and I want to get an access_token to get User Data Permission for items like
email
user_actions.music
user_activities
user_events
user_location
etc.
I know this can be done by using Graph API Explorer in Facebook developers. Can you show me how to do this in PHP? Thanks!
After include'ing the Facebook SDK for PHP, simply call $facebook->getLoginUrl with appropriate parameters.

Restricted Facebook Page Details over Graph API using PHP SDK

I'm trying to use Facebook's PHP SDK to get the number of likes for a fan page. This works fine for most pages, but some pages are returning false because they have age restrictions. Using the PHP SDK, how do I get the number of likes for an age restricted fan page over the graph API?
I'm currently not using an access token to read the pages. Would an app access token work? And if so how do I use an app access token with the PHP SDK.
Restricted pages can be read using the graph api but it requires a user's access token. I had tried it with an app access token which does not work.
The relevant documentation can be found here: https://developers.facebook.com/docs/reference/api/page/
You should to use Page access token. To get list of your pages with access tokens, use me/accounts method of the Graph API.

How to post to facebook wall once I've got the access token

I'm making a facebook app and have got as far as getting the user to allow my apps permissions, which then generates a code. Then using this code to get an access token. So I now have a variable in my PHP script containing the access token. Now how do I use this to post to the users wall?
The easiest way to post a message on the wall is through the graph api. You require the publish_stream permission to post status messages. You can simply do a POST with the desired message and the access token.
See the official facebook documentation for more details: http://developers.facebook.com/docs/reference/api/status/
Facebook changes their API so often, it's best to read their docs, if those are up to date...
Check in IRC #facebook as well.
But basically, you pass that token in to the API call somewhere, somehow and it's in their docs somewhere.
Unless FB has decided you don't need it or ignores it or doesn't want to allow that this week.

How to get a Facebook user's ID?

I followed this tutorial: http://developers.facebook.com/docs/authentication/
on how to setup Facebook authentication using php (server-side implementation). I can get a access token back from Facebook but how do I get the Facebook user ID of the user who has logged in?
You should use facebook PHP SDK:
https://github.com/facebook/php-sdk/
See the example file in order not to get Lost...
https://github.com/facebook/php-sdk/blob/master/examples/example.php
Facebook graph API is kinda annoying... so using this will be easier (at least for me)
The easiest way, if your framework/sdk/whatever doesn't provide it for you, would be to query https://graph.facebook.com/me?fields=id&access_token=....
If you're looking for a quick non-programatic way of getting your user-id, it's in your browser cookie called c_user I believe

Categories