Get user id in case Signed Request - Facebook - facebook

I know it might be duplicate question but please help. I have created a app and installed it on my Facebook page. My issue when I authenticate it using authentication method I got the user id... But when I go through proper signed request method (i.e. When I login with facebook site only.) I don't get user. how can I get userid in case of signed request that When I login using facebook site.
My signed request.
$facebook = new Facebook(array(
'appId' => '****',
'secret' => '****',
'cookie' => true,
));
echo 'User ID :- ' . $user = $facebook->getUser();
$signed_request = $facebook->getSignedRequest();
echo '<pre>';
print_r($signed_request);
echo '</pre>';
User ID :- 0
Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1372676199
[page] => Array
(
[id] => 544896778905441
[liked] =>
[admin] => 1
)
[user] => Array
(
[country] => in
[locale] => en_US
[age] => Array
(
[min] => 21
)
)
)
How can I get User id.

** EDIT **
As of a few months ago (Dec 2014) Facebook no longer allows Like-Gating content. I guess that's why someone is going around placing negative votes and links to their own answers. So the below is no longer valid
There is a difference between getting a signed_request for a user who has authorised an app, which is your first case and getting the user id from a signed request on a page app. In the second example you can test if a user likes your page, but you can't get the specific user id. They are just interacting with your page - yes they may be logged into Facebook, but they haven't explicitly authorised your app. I presume this is what you mean by "login with Facebook site only"
If you look at https://developers.facebook.com/docs/reference/login/signed-request/ you will see it states
Some fields and values, the user_id and oauth_token for example will only be passed if the user has logged into your app.
They may be logged into Facebook, but not have logged in/authorised your app

You can follow the instruction on how to decode the signed request here
https://developers.facebook.com/docs/facebook-login/using-login-with-games#parsingsr
OR
You can use the service that I created. It will return decoded signed request :)
https://websta.me/fbappservice/parseSignedRequest/<concat signed request here>
//angularjs
you may get it via $http.jsonp(url);
//jquery // refer to jquery docs on getting jsonp requests
$.getJSON( url, {
format: "json"
})

Related

Posting to my FB page as the page owner/admin, not my profile name/id

I have attempted to find an answer vis-a-vis the FB api, Google and this forum.
What I would like to know is what type of a value do I use for the 'from' field when posting to /PAGE/feed using the FB api.
I began including a 'picture' field and now posts to my page are happening as my fb account and not the page like it normally does.
You must first obtain the page access token before you can post as the page. You're going to need the manage_pages and publish_stream permissions too. To get the page access token, you should run the request using the page administrator's account.
assuming you've done the login and permission (scope) setting correctly, the following code might give you an idea of how this works. assuming $page_id is the variable holding your page's ID number.
<?php
if ($me)
{
$pageAccessToken = $facebook->api('/'.$page_id.'?fields=access_token');
if (isset($pageAccessToken['access_token']))
{
// switch to use page token instead of user token
$facebook->setAccessToken($pageAccessToken['access_token']);
// setup your post
$params = array('message' => "This is a test post",
'picture' => "http://nypsit.edu.sg/images/nypsit.jpg",
'name' => "NYP SIT Homepage",
'link' => "http://www.nyp.edu.sg/sit/school-of-information-technology",
'description' => "Nanyang Polytechnic - School of Information Technology"
);
// post to page timeline
$status = $facebook->api('/'.$page_id.'/feed', 'POST', $params);
// check if post successful
if (isset($status['id']))
{
echo "<h3>Message posted to page timeline!!!</h3>";
}
}
}
?>

how to post facebook user wall/ page status by a single user only no app install for other users

I am working on a site and i need to post page links from that site to a special user wall or page if something is published on it which means i only need one user to post that question.
the problem which i am facing is access token since i am not trying to show facebook login page in front of website traffic. its not like sharing on user wall we are basically trying to post status on a page automatically so any help for this problem will be appreciated.
i am using the following code which seems to work fine as long as the access token for the page is valid but i need to make something permanent so that i can add all the info in the php code hardcoded and it work for some weeks at-least without changing the api key or token
$appid = 'code';
$appsecret = 'code';
$wall= $pageId = 'code';
$token='page token';
$facebook = new Facebook(array('appId' => $appid, 'secret' => $appsecret, 'cookie' => true));
$params=array( 'message' => $msg,'name' => $title, 'caption' => $title, 'link' =>$uri,'description' => $desc, 'picture' => $pic, 'access_token' => $token,'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) );
$result = $facebook->api($wall.'/feed/','post',$params);
if i make token from the graph api debug page then it works fine but after some time i got the message "OAuthException: An active access token must be used to query information about the current user."
i am really desperate to find something solid for it.
thank you
You can write a backend script (maybe a cron job using the FB PHP-SDK) that runs as the special user and makes the desired FB api calls. For this, you will want to use a long-lived access token, which needs to be renewed every 60 days. You might also try your FB api calls using an App Access Token, which do not expire but also do not support all FB publishing and other operations.
To post to someones wall (or perform any action with the open graph) you must be authenticated with facebook as somebody in their graph or an application that they have granted permission to.
You should look at the permissions that can be set using the oauth scope attribute...
https://developers.facebook.com/docs/reference/dialogs/oauth/
To me it looks like they will need to grant your application publish_stream if you want to write to their friends walls as well as the user that you are authenticated as.
https://developers.facebook.com/docs/reference/login/extended-permissions/

Application post to its Wall

I can't find an example of exactly what I'm trying to do, so sorry if this is a repeated question, but I have looked for the last couple of hours.
I want my Facebook app to be able to update a status or post to a wall of a page that I administrate: http://www.facebook.com/amazingjobsapp as part of a CRON job.
I just can't seem to find any examples on how to do this. Any pointers are greatly appreciated.
EDIT:
I've used to code below and now am getting:
object(FacebookApiException)#11 (8) { ["result":protected]=> array(1) { ["error"]=> array(3) { ["message"]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["type"]=> string(14) "OAuthException" ["code"]=> int(200) } } ["message":protected]=> string(72) "(#200) The user hasn't authorized the application to perform this action" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(86)
Can someone walk me through granting my app manage_page and publish_stream permissions for page id 413282938687554 please?
EDIT 2: If I look at my graph.facebook.com/me/accounts then it shows the page id above with an access token but if I use that in my code then I still get the error.
You need to get the manages_pages permission
<?php
$attachment = array(
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://mylink.com',
'description' => 'this is a description',
'picture' => 'http://mysite.com/pic.gif',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com'
)
)
);
// To authenticated user's wall
$result = $facebook->api('/me/feed/', 'post', $attachment);
// To page's wall
$result = $facebook->api('page-id/feed/','post',$attachment);
// To user's wall
$result = $facebook->api('user-id/feed/','post',$attachment);
Ok maybe I missed something in the docs but I found the solution with is as follows:
Note this is for people who are trying to post from a cron job.
Grant the manage_pages, publish_stream and offline_access perms to
your app
Get the access token for your page from me/accounts in the graph api explorer
Add one simple line of code just after you initialize your fb object
$facebook->setAccessToken("XXXXX");
Note the access token needs to be the PAGE access token you got from step 2 and remember if you de-auth the app the access token will change.
Application will need to ask you for manage_pages & publish_stream permissions.
Obtain your pages page_access_token from the graph api /accounts call. Each page you own will have its own token listed. This token can be saved to database or accessed at the time of need. It is not recommended to expose or share this token.
Use "php-sdk" to make the post.
refer to: http://developers.facebook.com/docs/authentication/
refer to: http://developers.facebook.com/docs/reference/php/
NOTE: To impersonate the Page when posting to the wall (i.e. post as the Page, and not the current user), you must use a Page access_token with the manage_pages and publish_stream permissions, as described under Page Access Tokens above.
Example: Assumes user has installed and initialized php sdk for Facebook
<?php
if($user){
$vars = array(
'message' => "message goes here",
// you can uncomment below to enable an image with caption and link.
//'picture' => "image",
//'link' => "Link here",
//'name' => "Name here",
//'caption' => "Caption here",
'show_error' => true,
// comment out the access_token array below to post as a user instead of as page.
array('access_token' => YourPageAccessToken)
);
$testpost = $facebook->api('YourPageId/feed', 'post', $vars);
}
?>
If you find this answer helpful or is correct please remember to mark
it up or mark it as correct, so future users will have an easier time
finding answers.
Ok where I had thought we'd fixed it we haven't the post only works when i'm actually logged into the app itself.
I'm using Shawn's code with all the permissions mentioned with the access token from "me/accounts/" for the AmazingJobs Community page.
I've definitely granted manage_pages, publish stream and offline_acess to my own facebook account i then used the graph api explorer to find the access token within accounts and cut and pasted it into my code. Any ideas?

Facebook Graph api How to get page ID via an access token

For "users" on Facebook, I gather information like their user ID by using this graph api call:
https://graph.facebook.com/me?access_token=...
This works every time.
My question is... is there a way to get the page information about the page that was just authorized via the access token that is returned after authorization?
I tried this, and id doesn't work, but this is what I'm looking for
https://graph.facebook.com/page?access_token=...
As you know, in order to post to a "page" wall is to 'Post' like so...
https://graph.facebook.com/PAGEID/feed?message=blahblah&access_token=XXXXXXXXXXXXX
How do I get the page ID and other info about the page that was authorized without already knowing the ID or page "username" (which you have to have 25 likes to use anyway)???
Thank you for any help stackoverflow community:)
The answer I was looking for is here. You can simply use a users access_token to get a set of their page_access_tokens, and give them the option of posting to these pages using these access tokens based on the return JSON object data.
pages are not authorized, applications are.
in case that your application has permissions, the current page is informed thru the signed_request that FB passes to the app canvas. use the php sdk to read it
https://developers.facebook.com/docs/authentication/signed_request/
There's no callback when an application is installed on a page. However, if a user is using your application via a tab iFrame you can get that pages id from the signed_request. You could maintain this information as part of your own user session, and use it as you see fit.
The contents of a signed_request to a tab iFrame would be similar to the following (output of PHP print_r):
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => xxxxxxxxxx
[page] => stdClass Object
(
[id] => FAN_PAGE_ID // target page id
[liked] => 1 // is the user a fan
[admin] => // is the user an admin
)
[user] => stdClass Object
(
[country] => ie
[locale] => en_GB
[age] => stdClass Object
(
[min] => 21
)
)
)
For more information see:
http://developers.facebook.com/docs/authentication/signed_request/
You may also want to take a look at the manage_pages permission
Also, see here:
http://developers.facebook.com/docs/reference/api/
Page Login
You can impersonate pages administrated by your users by requesting the manage_pages permission.
Once a user has granted your application the "manage_pages" permission, the "accounts" connection will yield an access_token property for every page administrated by the current user. These access_tokens can be used to make calls on behalf of a page. The permissions granted by a user to your application will now also be applicable to their pages.

Facebook API - how to get page ID using Facebook API?

With Facebook API, within a custom app added to a fanpage (tab app), how do I get the fanpage ID?
Here's what I'm trying to achieve:
user goes to the full-width canvas app, he authorizes himself there and allows the app to add a tab to one of the fanpages he's administrating
every Facebook user going to that fanpage, sees the tab and the tab shows content specific to that fanpage (that's why I need to know the fanpage ID)
if fanpage administrator wants to change something, he goes to the full-width canvas and logs in to my service (here's where I store what he wants to show in his Fanpage)
From a parsed signed request (to an tab iFrame) you can get the page id - the contents would be similar to the following (output of PHP print_r):
stdClass Object
(
[algorithm] => HMAC-SHA256
[issued_at] => xxxxxxxxxx
[page] => stdClass Object
(
[id] => FAN_PAGE_ID // target page id
[liked] => 1 // is the user a fan
[admin] => // is the user an admin
)
[user] => stdClass Object
(
[country] => ie
[locale] => en_GB
[age] => stdClass Object
(
[min] => 21
)
)
)
For more information see:
http://developers.facebook.com/docs/authentication/signed_request/