如何读取facebook签署__来获取user_id [英] how to read facebook signed_request to get user_id
问题描述
根据 Facebook - 画布页面文档中的身份验证,他们说我们将获得一个包含JSON对象的 signed_request
。现在他们说 signed_request
可以通过 $ _ POST ['signed_request']
我同意为我工作。
According to Facebook - Authentication within a Canvas Page Document, they say that we will be getting a signed_request
which consists a JSON object. Now they say that signed_request
can be get through $_POST['signed_request']
I agree its working for me.
现在根据他们,如果用户登录,我将获得一个JSON对象值,如下所示: -
Now according to them if the user is logged in i will be getting a JSON object value like this:-
{
"expires":UNIXTIME_WHEN_ACCESS_TOKEN_EXPIRES,
"algorithm":"HMAC-SHA256",
"issued_at":UNIXTIME_WHEN_REQUEST_WAS_ISSUED,
"oauth_token":"USER_ACCESS_TOKEN",
"user_id":"USER_ID",
"user":{
"country":"ISO_COUNTRY_CODE",
"locale":"ISO_LOCALE_CODE",
...
}
}
现在我想获取 user_id
,所以我使用这段代码,但它不工作: -
Now i want to fetch the user_id
out of this so i am using this piece of code but its not working:-
if(isset($_POST['signed_request']))
{
echo 'YES';
$json = $_POST['signed_request'];
$obj = json_decode($json);
print $obj->{'user_id'};
}
只需打印 YES
。为什么是这样?
It just print the YES
. Why is it so?
我已经读过某处没有应用程序认证,我将无法提取 user_id
但是根据Facebook的说法,这是第一步,认证申请将是第四。我是新来的,如果有人可以帮助我,这将是很大的帮助。谢谢。
I have read somewhere that without app authentication i will not be able to extract the user_id
but according to the facebook, this is the 1st step and authenticating the application would be 4th. I am new to it, if somebody can please help me, it will be of great help. Thanks.
推荐答案
我认为在 json_decode($ json)
因为 $ json
不是一个有效的json字符串,正如你在 print_r($ _ POST ['signed_request']) code>。
I think it failed at json_decode($json)
because $json
is not a valid json string, as you've mentioned in comment about print_r($_POST['signed_request']);
.
根据 Facebook - Canvas页面文档中的身份验证, signed_request
参数已编码,并且解析 signed_request
string将产生一个JSON对象。
According to Facebook - Authentication within a Canvas Page Document, the signed_request
parameter is encoded and, parsing the signed_request
string will yield a JSON object.
如果您使用的是PHP SDK,正如Abhishek在评论中所说, $ facebook-> getSignedRequest();
会给你解码的json。
if you're using the PHP SDK, just as Abhishek said in the comment, $facebook->getSignedRequest();
will give you the decoded json.
查看此处了解有关签名请求的详细信息
look here for more details on the Signed Request
这篇关于如何读取facebook签署__来获取user_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!