I want that my Php script (myscript.php ) that i have deployed on a public host gets the Facebook user access token (with my facebook account) from Facebook. I don't want user interaction with login procedure where to inser user and pass. Is it possibile and how can be made? I understand the SDK is not useful for this scenario.
For now my script is:
require_once ('../php-graph-sdk-5.x/src/Facebook/autoload.php');
$fb = new \Facebook\Facebook([
'app_id' => 'myappid',
'app_secret' => 'mysecret',
'default_graph_version' => 'v2.10',]);
$helper = $fb->getRedirectLoginHelper();
$accessTokenEntity=$helper->getAccessToken();
try {
$accessTokenEntity=$helper->getAccessToken();
$response = $fb->get('/me', $accessTokenEntity->getValue());
}
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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
Copied from gitub example but i think it s not good for my scenario.
And the output is
"Fatal error: Call to a member function getValue() on null in ..."
Related
My function to list add_group_account id of a particular user ends up by receiving the following error.
Message: Unknown path components: /adaccountgroups
I'm following this guideline Facebook API documentation
public function tp()
{
$fb = new Facebook([
'app_id' => "621201298943758",
'app_secret' => "f0aa4ae743ef.................",
'default_graph_version' => 'v2.5',
'default_access_token' => "EAAI0ZBrLB7w4BAIUyoGjSB50ZBsXbPxYfCjk6WxPQIa.................................",
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get(
'/me/adaccountgroups',
'EAAI0ZBrLB7w4BAIUyoGjSB50ZBs.....................'
);
} 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();
}
As I have discovered there is no endpoint or function by the name "adaccountgroups"available even though it was mentioned in their own documentation.
To get the add account ID corresponding to a particular user account it has to be called in following pattern
curl
https://graph.facebook.com/v13.0/me/adaccounts?access_token=EAAI0ZBrLB7w...............
SDK
$response = $fb->get(
'/me/adaccounts',
'{token.....}'
);
Friends, I have a problem that is spinning around me. I am learning to use the Facebook API, I have 24 hours with this and I am already understanding how it works, I feel a little sorry because in reality it is easy the problem is that I do not understand much English so it is difficult for me.
The specific question is: How do I automatically publish in the selected groups and pages when I log in with Facebook? For example, of the following groups that I manage, I only select one.
The problem is that when the group is posted, the group id is necessary, using this code snippet.
$response = $this->fb->post('/123456789/feed', $linkData, $accessToken);
So? The specific question, How do I get that list of groups (and also for the pages) to which I granted permission to be able to (it occurs to me), go through an array and do the post automatically.
Right now I am clear about the following points:
1.- I need to create an app: https://developers.facebook.com/
2.- Have the Facebook SDk installed: https://github.com/facebook/php-graph-sdk
3.- My class has saved these permissions and this configuration:
require_once "/../vendor/autoload.php";
class facebookController{
private $appid ='XXXXX';
private $appsecret='XXXXXXXXXXXXXXXXXXXXXXXXXX';
public $fb;
public $helper;
public $permisos = ['email', 'manage_pages', 'publish_pages' , 'publish_to_groups', 'user_posts', 'groups_access_member_info'];
public $callbackurl = 'http://localhost/misitio.com.mx/auth/fb-callback.php';
public $loginurl;
function __construct(){
$this->fb = new Facebook\Facebook([
'app_id' => $this->appid,
'app_secret' => $this->appsecret,
'default_graph_version' => 'v3.2',
]);
$this->helper = $this->fb->getRedirectLoginHelper();
$this->loginurl = $this->helper->getLoginUrl($this->callbackurl, $this->permisos);
}
}
4.- I must obtain the Access Token, it can be debugged here: https://developers.facebook.com/tools/explorer?method=GET&path=me%3Ffields%3Did%2Cname&version=v3.2
$this->helper = $this->fb->getRedirectLoginHelper();
try {
$accessToken = $this->helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
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;
}
5.- For some cases I need the user_id, I already have it saved:
$oAuth2Client = $this->fb->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$userid = $tokenMetadata->getUserId();
6.- To post for example in a group, I use the following and this is where the inconvenience is: (Based on the official documentation: https://developers.facebook.com/docs/graph-api/reference/v3. 2 / user / groups)
public function toPost($titulo, $url){
$empresa = new configuracionController();
$userid=$empresa->getFbUserId();
$accessToken = $empresa->getFBToken();
try {
// Returns a `Facebook\FacebookResponse` object
$this->fb->post('/123456789/feed', $url, $accessToken);
} 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();
}
From already thank you very much.
I'm trying to display facebook status updates for a sports team on their website using the facebook graph api, but can't seem to get a valid access token.
Here's my code.
require_once APPPATH.'/third_party/facebook-php-sdk-v4/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123123123123',
'app_secret' => 'fghdfghtyjdfghdghjfghjfghj',
'default_graph_version' => 'v2.5'
]);
$fb->setDefaultAccessToken('123123123123|dfhjfgytdfghdfhgdsfjd');
$helper = $fb->getCanvasHelper();
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.
} else {
echo 'access token not set - THIS IS WHERE I ALWAYS SEEM TO END UP';
}
exit;
Is there anything obvious I'm doing wrong?
This is what I used in a Laravel 4 app to connect. Basically when the user clicks on a 'Log in' button,
1- Create the Facebook instance with app_id, app_secret and default_graph_version.
2- Get a redirectLoginHelper object to get a loginUrl with a code that facebook will provide when you call $helper->getLoginUrl($redirectUrl, (array) $permissionsNeeded)
3- Redirect to that url
4- Create facebook instance and getRedirectLoginHelper again as step 1 and 2
5- Check if you received the 'code' parameter in the url the helped returned
6- Get the access token: $accessToken = $helper->getAccessToken()
In the code I'm providing, I do all this steps in one method. I check if there's a code parameter in the request, if not I request it and redirect. The next time the code runs, when I check if code is there, it continues to the next step
$fb = new Facebook([
'app_id' => 'facebook_app_id',
'app_secret' => 'facebook_app_secret',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
if(Input::get('code') == '') {
$permissions = ['manage_pages, publish_actions'];
$loginUrl = $helper->getLoginUrl('http://yoururl.dev/uri', $permissions);
return Redirect::to($loginUrl);
}
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
//Handle error
}
In this case, the Log in button goes to www.domain.app/connect, the URL that the redirectLoginHelper returns is the same, but with the code parameter: www.domain.app/connect?code=ntasd341251
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;
}
}
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.
I am use this code from Facebook
https://developers.facebook.com/docs/php/gettingstarted/5.0.0
But now its show Facebook SDK returned an error: Cross-site request forgery validation failed. The "state" param from the URL and session do not match.
I cant understand whats wrong
my login callback page code
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxx',
'app_secret' => 'xxxxxxxx',
'default_graph_version' => 'v2.2',
]);
$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!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
$_SESSION['facebook_access_token'];
}
Insert this code after: $helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
and it will work or for more detail visit facebook login apps
You are likely not accessing your server using the domain registered to the app. Are you running your webserver on localhost? If so, edit your /etc/hosts file to include something like
127.0.0.1 local.<yourdomain>.com
and then go to local..com and that should take care of it