i'm using sdk php to post a link. here is my function (https://developers.facebook.com/docs/php/howto/postwithgraphapi/)
public function shareFB(Request $request, $slugify)
{
$petition = Petitions::where('slug', $slugify)->first();
// $access_token = auth()->user()->social_account->access_token;
$fb = new Facebook([
'app_id' => config("services.facebook.client_id"),
'app_secret' => config("services.facebook.client_secret"),
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$linkData = [
'link' => route('petitions.index',$petition->slug),
'message' => $request->get('comment-fb'),
];
try {
$response = $fb->post('/me/feed', $linkData, '$access_token');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$share = $response->getGraphNode();
return redirect()->route('petitions.index',$petition->slug);
}
and error is Invalid OAuth access token, maybe i get wrong access token. Anyone help me solve that/ Thank u
Related
i'm creating function to share a post to facebook and i'm following docs : https://developers.facebook.com/docs/php/howto/postwithgraphapi. but it's not work.
This is my function
public function shareFB(Request $request, $slugify)
{
$petition = Petitions::where('slug', $slugify)->first();
$access_token = auth()->user()->social_account->access_token;
$fb = new Facebook\Facebook([
'app_id' => config("services.facebook.client_id"),
'app_secret' => config("services.facebook.client_secret"),
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => route('petitions.index',$petition->slug),
'message' => $request->get('shareFB'),
];
try {
$response = $fb->post('/me/feed','post', $linkData, $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$share = $response->getGraphNode();
}
and here is my route in laravel
Route::post('share/p/{slugify}','Frontend\SocialController#shareFB')->name('social.shareFB');
When i clicked a button have href above, it's show MethodNotAllowedHttpException . somebody help me solve that!!!
I wrote a code for post a message on my page (not own timeline)
post.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
$response = $fb->get('/xxxxxxxxxxxxxx?fields=id,name');
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
//exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'manage_pages', 'publish_actions'];
$loginUrl = $helper->getLoginUrl('http://xxxxxxxxxxxxxx/facebook/login-callback.php', $permissions);
echo 'Log in with Facebook!';
login-callback.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => 'APP-ID|APP-SECRET'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
} elseif ($helper->getError()) {
// The user denied the request
}
header('Location: index.php');
It's just work when in file post.php use this code:
$response = $fb->post('/me/feed', $data, $_SESSION['facebook_access_token']);
And then post show on my timeline.
But when I want to post on page and use this:
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
don't work and not appears anythings, and don't show my post on the page or timeline!
Here's my code to retrieve user's info from Facebook. I am using PHP SDK.
public function indexAction()
{
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'default_graph_version' => 'v2.5',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
if(isset($_SESSION['facebook_access_token'])){
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
// echo 'Name: ' . $user['first_name'];
$this->_redirectUrl('/app-new/app/#/myAccount');
exit;
}
//redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl(Mage::getBaseUrl().'facebook/facebook/authenticate', $permissions);
echo $loginUrl;
}
public function authenticateAction(){
Mage::log("Authenticate=======");
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in!
Mage::log("Access Token=================>".(string) $accessToken);
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$fb->setDefaultAccessToken((string) $accessToken);
$_SESSION['facebook_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
$customer = $this->checkIfUserExists($userNode);
$this->_redirectUrl('/app-new/app/#/myAccount');
exit;
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
}
I got the result first, but I am not getting result after that. Is it due to access taken or something else. What am I doing wrong here
You are missing a new feature of v2.4 of the Graph API, it´s called "Declarative Fields". You now have to add a field parameter to specify the fields you want to get, else you will only get id and name: https://developers.facebook.com/docs/apps/changelog#v2_4
Also, make sure your login works correctly and you get asked for the email permission when you authorize your App.
I don't why I am getting this error.
My appid and secret id are all correct. Don't know what went wrong.
<?PHP
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('C:/xampp/htdocs/laravel5-learning/public/image/share/'.$name),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/photos', $data, '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
?>
It is showing this error:
Graph returned an error: Invalid OAuth access token.
I would like to use the Facebook PHP SDK v5 to automatically post to a FAN PAGE. I have the code below which successfully posts to my own wall, but how do I alter the code to post to my fan page? From what I've read, I need to pass in the page id of the fan page?
$params["message"] = 'test';
$params["link"] = 'https://example.com';
$params["picture"] = 'https://example.com/images/logo_hod.jpg';
$params["description"] = 'testtt';
$access_token = $accessToken;
try {
$response = $fb->post('/me/feed', $params, $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
echo 'Posted with id: ' . $graphNode['id'];
I tried replacing this line:
$response = $fb->post('/me/feed', $params, $access_token);
with this to substitute in the fan page id:
$response = $fb->post('/229107363768165/feed', $params, $access_token);
and got the error:
Graph returned an error: Unsupported post request.
UPDATE: I also made the app "public" in an attempt to get past the "unsupported post request" error. No luck.
i think your $params is not right.
I use this:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
$params = [
'link' => 'https://example.com',
'photo' => 'https://example.com/images/logo_hod.jpg',
'message' => 'A test post!',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/me/feed', $params, '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
Post Links Using the Graph API
I assumes that you've already obtained an access token and the access token must have the publish_actions permission for this to work.