I'm trying to get a the facebook userid in a facebook tab, but somehow this always returns 0.
This is the code I use:
$config = array();
$config["appId"] = "427761887242287";
$config["secret"] = "xxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook($config);
$fbData = $facebook->getSignedRequest();
Facebook appId and secret are valid, I've already tried with resetting the secret.
I've tried to do a print_r on $fbdata but this only returns:
Array
(
[algorithm] => HMAC-SHA256
[issued_at] => 1337603346
[page] => Array
(
[id] => 158282080958121
[liked] => 1
[admin] =>
)
[user] => Array
(
[country] => nl
[locale] => en_US
[age] => Array
(
[min] => 21
)
)
)
As you can see, this retuns only some non usefull data as far as the user goes.
I've also tried $facebook->getUser() but this does return 0.
I do have a valid SSL certificate on my server, I use the right http and https URL's but I can't find out why I get a 0 for the userid while I'm logged in.
Any suggestions?
If this is a facebook page tab application, then the user id is not returned unless the user has explicitly authorized your application. And from the response you've posted it looks like this is the case. You would need to get the user to authorize your application, not just like a page for this to work
https://developers.facebook.com/docs/authentication/pagetab/
Related
I am trying to get app_data from the facebook canvas.
It's working in page tab.
https://www.facebook.com/something/app_55218516481307?app_data=4B9E753
$signed_request = $facebook->getSignedRequest();
$app_data = $signed_request["app_data"];
app_data contains the value - '4B9E753' So, it's nice.
But there, app_data is not even set.
https://apps.facebook.com/namespace/?app_data=something
signed_request contains only
Array (
[algorithm] => HMAC-SHA256
[issued_at] => 65456446
[user] => Array (
[country] => cz
[locale] => cs_CZ
[age] => Array (
[min] => 21
)
)
)
Why?
It only works in a page tab and not on the canvas page.
https://developers.facebook.com/docs/reference/login/signed-request/
to send data to the app on its canvas app page use
'https://apps.facebook.com/yourappname?sk=app_yourapp_id/whatever_you_wish_to_send/'
and get it in php with $thisrequest = $_REQUEST['sk']; you can echo it to see what you get back.
I have a page tab app. When the user clicks on the "Go to App" and is sent to my page tab edit url i am trying to determine if they are a page admin or not.
I have tried two different methods. I have tried from the only admin/owner of the page
method 1 used from https://developers.facebook.com/blog/post/2011/09/05/platform-updates--labor-day-edition/
$page_info = $facebook->api("/".$pageID."?fields=access_token");
$pageAccessToken = $page_info['access_token']
$is_admin_url = "https://graph.facebook.com/" . $pageID
. "/admins/" . $FBuser . "?access_token="
. $pageAccessToken;
$response = file_get_contents($is_admin_url);
response is {"data":[]}
I have also tried::
path = '/'.$pageID.'/admins/'.$FBuser;
$params = array(
'app_id' => FB_APP_ID,
'access_token' => $pageAccessToken
);
$is_admin = $facebook->api($path, 'POST', $params);
Although PAGE_ID/admins is a valid request, you need an admin's access_token to see the list. I.e. only admins can see who else is an admin.
What you can do is approach this from the other end by yielding a list of pages that the user is an admin of (using the https://graph.facebook.com/USER_ID/accounts/ data and the manage_pages permission) and search through that list for your application.
However, I would understand if some users would be reluctant to give the manage_pages permission, as it also provides an access token to authenticate as that page, which would be something of a security hole on their part. Unfortunately, there does not seem to be another way to access a list of pages for which that user is an admin.
Simplest way will be signed Request.
A signed_request parameter is POSTed to an application when the app is loaded inside a Page Tab.
You can get a lot of information from signed_request
require '../fb_sdk/facebook.php';
$config = array();
$config['appId'] = '45916xxxxxx';
$config['secret'] = '59caxxxxxx';
$facebook = new Facebook($config);
$facebook->setFileUploadSupport(true);
$signed_request = $facebook->getSignedRequest();
print_($signed_request); will have output like
Array
(
[algorithm] => HMAC-SHA256
[expires] => 1347210000
[issued_at] => 1347203265
[oauth_token] => AAAGhmv67ki8BAAfBwtxxxx
[page] => Array
(
[id] => 192430xxxxxx
[liked] => 1
[admin] => 1
)
[user] => Array
(
[country] => in
[locale] => en_US
[age] => Array
(
[min] => 21
)
)
[user_id] => 10000020xxxxx
)
You can use $signed_request[page][admin] value to determine whether a user is admin of current page, in which your app in loaded in Page Tab. If it's set to 1 then user is admin of the page else not an admin i.e. set to 0.
More About Signed Request
i had some luck using admin_only = true
https://developers.facebook.com/docs/graph-api/reference/user/groups/
Is there anyway to get the state information of the user visiting my facebook tab page? Currently via signed request i can get the country information. Is there a way to get state information also??
Answer is NO, check out the documentation here https://developers.facebook.com/docs/authentication/signed_request/
You get data like this
Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1328803958
[page] => Array (
[id] => 114951721840
[liked] => 1
[admin] => 1 )
[user] => Array (
[country] => us
[locale] => en_US
[age] => Array ( [min] => 21 )
)
)
This is data of signed_request:
Array
(
[algorithm] => HMAC-SHA256
[issued_at] => 1331219385
[page] => Array
(
[id] => xxxx602xxx551
[liked] => 1
[admin] => 1
)
[user] => Array
(
[country] => at
[locale] => de_DE
[age] => Array
(
[min] => 21
)
)
)
but user_id is missing ...
is this because user dont gave permission to the app?
I thougt an app can access all data which is public, like name or user_id
is there a way to get "user_id" without a basic permission?
You will not be able to get any identity of user prior to connection with application, this is just not possible.
You need to authorize the user to get his user id
Is it possible to programmatically add an event to a page using Facebook Graph API?
If yes, what HTTP request shall be made?
For example, Startup Weekend has events on its Facebook page.
These events can be added using Graph API Event object?
UPDATE
Creating event with the API is no longer possible in v2.0+. Check: https://developers.facebook.com/docs/apps/changelog#v2_0
Yes it's possible.
Permissions:
create_event
manage_pages
So first you get the page id and its access token, through:
$facebook->api("/me/accounts");
The Result will be something like:
Array
(
[data] => Array
(
[0] => Array
(
[name] => Page Name
[category] => Website
[id] => XXXXXX
[access_token] => XXXXX
)
[1] => Array
(
[name] => Page Name 2
[category] => Company
[id] => XXXXXXX
[access_token] => XXXXXXXX
)
)
)
UPDATE: To retrieve the page's access_token you could now call the page object directly like:
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
The access token, if successful call, should be accessible: $page_info['access_token']
Now you get the Page ID and access token and use the events connection:
$nextWeek = time() + (7 * 24 * 60 * 60);
$event_param = array(
"access_token" => "XXXXXXXX",
"name" => "My Page Event",
"start_time" => $nextWeek,
"location" => "Beirut"
);
$event_id = $facebook->api("/PAGE_ID/events", "POST", $event_param);
And you are done! :-)