PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 2, 2022

[FIXED] Share link in facebook with php-sdk

 March 02, 2022     facebook, facebook-graph-api, facebook-php-sdk, php     No comments   

Issue

I'm trying to share a link on Facebook with the php-sdk. Using Graph API Explorer in https://developers.facebook.com/tools/explorer and choosing the app that I create and the admin user for the page, it give me the access token and can share the link perfectly, but from the code with the same token I always get the error

Graph returned an error: Invalid OAuth access token.

I understand that is an authentication error but, avoid the authentication is not the purpose of use the token?, if not, how can be logged the admin user for do the task and why is not mentioned in https://developers.facebook.com/docs/php/howto/example_post_links/5.0.0 .The idea is to share the link with the user page administrator, not with the user logged on my site. This is the code.

public function shareOnFacebook($link)
{
    $pageId = "XXXXXXXXXXX";
    $accessToken = "YYYYYYYYYYYYYY";
    $appId = "ZZZZZZZZZZZ";
    $appSecret = "SSSSSSSSSSSSS";

    $fb = new Facebook([
        'app_id' => '{'.$appId.'}',
        'app_secret' => '{'.$appSecret.'}',
        'default_graph_version' => 'v2.2',
    ]);

    $linkData = [
        'link' => 'http://www.example.com',
        'message' => 'User provided message',
    ];

    try {

        $response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');
    } catch(FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}

Solution

You should replace the whole field with your access token:

$response = $fb->post('/'.$pageId.'/feed', $linkData, '{'.$accessToken.'}');

Should be

$response = $fb->post('/'.$pageId.'/feed', $linkData, $accessToken);

I suppose adding the access token to the $linkData should work as well.



Answered By - ifaour
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing