Cristhian Boujon
Cristhian Boujon

Reputation: 4190

Facebook SDK - Get access token programatically

I have a web application made with PHP and when I submit a particular form, I want to publish to my Facebook fan page. You can see the below code:

    $appId = "...";
    $appSecret = "...";
    $version = "v2.5";

    $fb = new Facebook\Facebook([
        'app_id' => $appId,
        'app_secret' => $appSecret,
        'default_graph_version' => $version,
    ]);

    $accounts = $fb->get("/me/accounts");
    $pages = $accounts->getGraphEdge()->asArray();

With that code I have the FacebookSDKException saying that You must provide an access token. I have not found a way to generate the access token programatically just using https://developers.facebook.com/tools/explorer. My composer:

"require": {
        "facebook/php-sdk-v4" : "~5.0"
}

How can I get an access token programatically/automatically? Or what alternative do you suggest?

Upvotes: 0

Views: 4352

Answers (1)

Dimitris Papageorgiou
Dimitris Papageorgiou

Reputation: 447

here is what I did. For start use the code of the JS SDK here

If response.status===connected(find that expression in the code) obtain the access token with this code:

 var accessToken = response.authResponse.accessToken;

then redirect to a page where PHP code resides and in the url attach the access_token...like this:

  window.location = "http://localhostfb_test.php?access_token="+accessToken;

In the server now use the token like this.

$user_profile = $fb->get('/me',$_GET['access_token']);

here

and in the section Tokens are Portable it is clearly mentioned that tokens can shipped from client to server...this is what I do here.

Upvotes: 3

Related Questions