简体   繁体   English

如何在Facebook canvas应用程序授权期间获取用户名?

[英]How to get user name during facebook canvas application authorization?

I am successfully able to authorize my canvas app using this code but I am only able to get the user id.I need user name,age friendlist also.Plz help me. 我可以使用此代码成功授权我的canvas应用,但我只能获取用户ID。我还需要用户名,年龄朋友列表。请帮我。

I used Print_r and get this 我用了Print_r并得到了

Array ( [algorithm] => HMAC-SHA256 [expires] => 1366020000 [issued_at] => 1366012878 [oauth_token] => BAAFaFlrFWXYBADDEIui8rTtcEWldiZAGlHZB1mGSZoQ6E9fU9vkweYnx2jvj4SSDNRUd465YcAiz4hI3jlQlXLBmB0jVXZBeoAiSyO5P2TQoIQVt9SQRWVJEuTGtcyQzazZCi20MWhDqBsa1qgYWKloSRWOkKBdPDBZCI0exZA13Jygh73hsr9ldVvFy8ClgEb8jIZBblCArGT3lb7g [user] => Array ( [country] => pk [locale] => en_US [age] => Array ( [min] => 21 ) ) [user_id] => 100000 

PHp code 邮递区号

 $app_id = "    xxxxxxxxx";

$canvas_page = "xxxxxxxx";

$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page);

$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), TRUE);

if(empty($data["user_id"])) {



    echo("<script> top.location.href='" . $auth_url . "'</script>");

} else {

    print_r($data) ;
    echo ("Welcome User: " . $data["user_id"]);
    echo ("Welcome User: " . $data["age"]); //i tried this,but got undefined message?

}

You can access the user endpoint in the Graph API sending a signed request, for example querying https://graph.facebook.com/me?fields=id,name will give you the user name of the authenticated user. 您可以在Graph API中发送已签名的请求来访问用户端点,例如查询https://graph.facebook.com/me?fields=id ,名称将为您提供经过身份验证的用户的用户名。 See http://developers.facebook.com/docs/reference/api/user/ for reference. 请参阅http://developers.facebook.com/docs/reference/api/user/以获取参考。

I suggest you use the Facebook PHP SDK 我建议您使用Facebook PHP SDK

you can get it from github and it makes things easier. 您可以从github获得它,这使事情变得更加容易。 I believe it has a basic example that does just what you want. 我相信它有一个可以满足您需求的基本示例。

HTH 高温超导

You need to do an API request or use FQL in order to get more information about the user. 您需要执行API请求或使用FQL,以获得有关用户的更多信息。 http://developers.facebook.com/docs/reference/php/facebook-api/ has examples that show you how to get the user's name with using the API method. http://developers.facebook.com/docs/reference/php/facebook-api/提供了一些示例,向您展示了如何使用API​​方法获取用户名。 And then here is the API documentation for FriendsList, and here is the FQL docs. 是FriendsList的API文档, 是FQL文档。 Make sure you have the read_friendlists permission. 确保您具有read_friendlists权限。 Let me know if you have any questions :) 有任何问题请告诉我:)

Example with code: 代码示例:
Follow the instructions at http://developers.facebook.com/docs/reference/php/ to install the PHP SDK from https://github.com/facebook/facebook-php-sdk/ . 按照http://developers.facebook.com/docs/reference/php/上的说明从https://github.com/facebook/facebook-php-sdk/安装PHP SDK。

Then go to http://developers.facebook.com/docs/reference/php/facebook-api/ and copy the first example into a file. 然后转到http://developers.facebook.com/docs/reference/php/facebook-api/并将第一个示例复制到文件中。 Make sure to update the app id, app secret, and that your require_once is set to right location. 确保更新应用程序ID,应用程序密钥,并且您的require_once设置在正确的位置。

Then replace 然后更换

  try {

    $user_profile = $facebook->api('/me','GET');
    echo "Name: " . $user_profile['name'];

with

try {
        //Get User Name
        $user_profile = $facebook->api('/me','GET');
        //Get User's Friends
        $friends_list = $facebook->api('/me/friends', 'GET');
        echo "<h1>Name: " . $user_profile['name']."</h1>";
        echo "<h2>Friends:</h2>"; 
        foreach ($friends_list['data'] as $friend)
        {
            echo $friend['name']."<br>";
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM