Facebook PHP SDK 3 issue in access_token - php

I am using facebook PHP SDK. After the user allows my application, facebook redirects to may web page.
Following is the URL
http://Mydowmain.com/facebook.htm?state=d96afbb9ea550fg81c287b7822cee0af1&code=AQDtl7Rd7qgCp6S_dXwOCxxfXo1dEh75ZJqrCINMawCUzxOYs9JnpmWKMoYTgjuNhQx2nk8pleCdvGqSt39F2pHKMHt3aNxwqoIah2oshPKKC0MnWycPSU0yLAKMYVQV2g3jRQdEMf2dWTVUrk_ammSpGaYbfFQ3GeccZ1vGqfVazsaQ_1Lxz7GjITlqgCxrOlc#_=_
I need to fetch the access_token
Following is the code
if($_GET['code'] != '')
{
$access_token = $facebook->getAccessToken();
When i try to use this $access_token to get the user details.
$user_profile = 'https://graph.facebook.com/me/?access_token='.$access_token;
It gives the following error
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
}
I dnt what i am doing wrong, it was working with SDK 2 but not working with SDK3
$access_token = $facebook->getAccessToken();
this Call Returns the following, in which fistpart before "|" is App ID and After "|" is App Secret..
187997367916887|9d87d7a2d26485fd926a000b0c3b2f87

Check this link out, scroll down to the answer with the following in bold: "When using your Facebook Application's token", hope it helps.
Facebook access_token invalid?

Change your graph call's api ('/me') by ('/[number or name id]')

Related

Facebook PHP - posting error to timeline but not to page

I am trying to post to a Facebook account (with permissions) using the PHP
API. I am giving our users two options - posting to the Feed, and posting
to a specific facebook page. I am getting an error in the first case, but
not in the second case. In the code below:
$access_token is the access token I got back from linking to my app. It's of type
"User", and has the following permissions:
email
pages_show_list
business_management
pages_read_engagement
pages_manage_metadata
pages_read_user_content
pages_manage_ads
pages_manage_posts
pages_manage_engagement
public_profile
Where $page_token is, well, a "Page" type token, with the same set of permissions.
When I post to the page, it works fine. But when I try to post to the timeline, I get
the error:
Graph Error 200 : [Timeline]: [(#200) If posting to a group, requires app being
installed in the group, and either publish_to_groups permission with user token,
or both pages_read_engagement and pages_manage_posts permission with page token;
If posting to a page, requires both pages_read_engagement and pages_manage_posts
as an admin with sufficient administrative permission]
I'm not trying to post to a group, I'm posting to an account's timeline. And, per the
access token debugger, I have the permissions requested anyway. What's going on?
My code is below:
$linkData = [
'link' => $link_to_post,
'appsecret_proof' => $app_secret_proof
];
if ( POSTING TO TIMELINE )
{
$token = $access_token;
$post_to = "/me/feed";
}
else
{
$page_id = $page_id;
$token = $page_token;
$post_to = "/$page_id/feed";
}
// THIS THROWS THE EXCEPTION:
$response = $this->fb->post($post_to, $linkData, $token);
You can not post to a personal timeline via API any more, that was removed ages ago already. (With introduction of API v2.4, if I remember correctly.)
You can only use the Share or the Feed dialog, to offer the user a way to actively share / make a post themselves, https://developers.facebook.com/docs/sharing/web

How to get data from facebook Ads on the Graph API?

I am new to PHP and facebook API'S
I am trying to get data from my ads account on faceobook using PHP
I just want to know how much $ every ads spent
First of all - I did not understand if I must have facebook app in order to get data from my personal ad account ?
I assumed yes so I created one now..,
I am geting the acess token like this :
require_once("facebook.php");
$config = array();
$config['appId'] = 'myapp_id';
$config['secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
// Set a new app secret
$facebook->setApiSecret($config['secret']);
// If you do above, also set the app id
$facebook->setAppId( $config['appId']);
$access_token = $facebook->getAccessToken();
But when I am trying a get request like this:
https://graph.facebook.com/act_[my_account_number]/adgroups
I am getting a premisson exception that tell me
An access token is required to request his resource
But where do I put the access token ?
You need to add it at the end of the url as a url parameter:
"https://graph.facebook.com/act_[my_account_number]/adgroups" . "?access_token=" . $access_token
However, from the code I think you are getting an App Access Token, and you need a Page Access Token or a User Access Token to get the ads related to your Facebook Page.
Please refer to https://developers.facebook.com/docs/facebook-login/access-tokens/ for more information.

OAuthException: An active access token must be used to query information about the current user

I've this error, but not always. (I use PHP SDK, latest version).
If I'm logged into facebook and i try to register and login, then the app say it's ok!
Also if i'm not logged into facebook, the app redirect me to login url, and here it's all ok.
But sometimes the app say this exception: OAuthException: An active access token must be used to query information about the current user. and the script redirect the user to loginUrl with a loop-redirect (because the access token isn't valid and user need always of loginUrl)
In the web some say that Facebook create a duplicate of Access Token and then access token of php sdk don't is = facebook.
For fix, the user must deletes cookies, how I can fix this?
Thanks a lot for reply, if code is need reply and I'll post it, have a good day! :)
Try to destroy the Facebook session, then redirect the user to login URI if you receive this error, this way you will get a fresh access token.
Notice: Access token expire after a very short, you can get a long living access token. The latest PHP SDK contains a public function called : setExtendedAccessToken()
you can call this function to automatically exchange the 2-3 hours living access token for a 60 days access token
Eg:
try {
$user = $this->facebooknew->api('/me');
$this->facebooknew->setExtendedAccessToken();
$access_token = $this->facebooknew->getAccessToken();
}
catch(FacebookApiException $e){
$this->facebooknew->destroySession();
header('Location: ' . $this->facebooknew->getLoginUrl($params));
//echo $e;
}

Facebook PHP SDK

I would like to know whether the Facebook PHP SDK is up to date with regards to Facebooks Server Side Authentication (https://developers.facebook.com/docs/authentication/server-side/)
I am having difficulty obtaining an access_token when FB passes back my state and code.
Am I meant to programmatically add this functionality? Or is the SDK meant to handle the response from Facebook?
example:
I pass FB:
https://www.facebook.com/dialog/oauth?client_id=1234567890&redirect_uri=http%3A%2F%2Flocalhost%2F&state=ef8dba2dc8b4fa002973ffa5e29c67cb
Facebook returns:
http://localhost/?state=ef8dba2dc8b4fa002973ffa5e29c67cb&code=AQAhuVUqufbghj2bBLcCr5SdRJopdYaR-vAoZJzilvvDgiGYNuEPVKTRLqD9p9MPrPhIIubYPH0aka8RtBQU_vSGdkCI5DkQxhQpwWcADu1_jSQgOC9vBsF2oWardpcPcq4PzmNq-JYhdvEFyMD0K7vPY0WioUgLghhw28FJAhHis9K7dz6HyrNy0YXfjbK0AN0#_=_
However the SDK does not handle this response above from Facebook.
Facebook suggests you obtain and access token by passing back the code.
Exchange the code for a user access token
Once the user has authorized your app, you should make a server side request to exchange the code returned above for a user access token.
https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID
&redirect_uri=YOUR_REDIRECT_URI
&client_secret=YOUR_APP_SECRET
&code=CODE_GENERATED_BY_FACEBOOK
Does the SDK not do this itself?
Try this :
$user = $facebook->getUser();
$url = $facebook->getLoginUrl(array(
'scope' => 'email,publish_stream,publish_actions',
'redirect_uri' => 'login.php', //give ur apropriate page
));
if($user){
$acestokn = $facebook -> getaccesstoken();
}else{
header('location:'.$url);
exit;
}

Authorizing a Facebook Fan Page for Status Updates

I'm able to update the status on my PROFILE wall using this code:
require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook('APP API KEY','APP SECRET KEY');
$user_id = 'MY USER ID';
$facebook->api_client->users_setStatus('This is a new status');
...after authorizing using this address:
http://facebook.com/authorize.php?api_key=MYAPPAPIKEY&v=1.0&ext_perm=publish_stream
This code, however, does not work to update the status on my Facebook PAGE Wall. Are there additional parameters that I can add to the authorize.php url to specify authorizing the PAGE and not just my profile?
Or, are there better ways to post updates to Fan Page Walls?
Thanks!
I solved the problem by consulting the Facebook desktop application documentation (even though this is a web application).
I first had to authorize offline access with this url (replacing 'MYAPIKEY'):
http://www.facebook.com/login.php?api_key=MYAPIKEY&connect_display=popup&v=1.0&next=http://www.facebook.com/connect/login_success.html&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access
Then, I needed to grant 'publish_stream' permissions to the PAGE with this url (replacing 'MYAPIKEY' and 'THEPAGEID'):
http://www.facebook.com/connect/prompt_permissions.php?api_key=MYAPIKEY&v=1.0&next=http://www.facebook.com/connect/login_success.html?xxRESULTTOKENxx&display=popup&ext_perm=publish_stream&enable_profile_selector=1&profile_selector_ids=THEPAGEID
I could then use the following code to publish to the Fan Page wall:
require_once 'facebook-platform/php/facebook.php';
$facebook = new Facebook(MYAPIKEY, MYAPISECRET);
try{
$facebook->api_client->stream_publish('INSERT_STATUS_HERE',null,null,null,'THEPAGEID');
}catch(Exception $o ){
print_r($o);
}
Based on the above, i tried out a couple of querystring parameters on the graph API authorize URL, and it appears this works:
https://graph.facebook.com/oauth/authorize?client_id=[APP_ID]&redirect_uri=[REDIRECT_URL]&scope=publish_stream&enable_profile_selector=1&profile_selector_ids=[PAGE_IDS]
EDIT: Never mind, the above displays all the UI correctly, but still get the "(#200) The user hasn't authorized the application to perform this action" error --- it's clear it doesn't work because the access token contains my USER id but not the page's ID.

Categories