javascript 通过用户ID获取用户名和姓氏FB Api
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16680364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get user name and surname by user id FB Api
提问by Bernice
In the Facebook API is there a way in which I can get a user's name and surname by user id?
在 Facebook API 中有没有一种方法可以通过用户 ID 获取用户的姓名和姓氏?
I am using FB API in my ASP.NET MVC application and I am using the MultiFriendSelector to let the users invite friends to my app. I need that when these friends are selected, they are saved in my database so that they can have access to my pages. This is the code I have until now.
我在我的 ASP.NET MVC 应用程序中使用 FB API,我使用 MultiFriendSelector 让用户邀请朋友加入我的应用程序。我需要当这些朋友被选中时,他们被保存在我的数据库中,以便他们可以访问我的页面。这是我到现在为止的代码。
function sendRequestToRecipients() {
var user_ids = document.getElementsByName("user_ids")[0].value;
FB.ui({
method: 'apprequests',
message: 'Join my group on CodeShare!',
to: user_ids
}, requestCallback);
}
function sendRequestViaMultiFriendSelector() {
FB.ui({
method: 'apprequests',
message: 'Join my group on CodeShare!'
}, requestCallback);
}
function requestCallback(response) {
if (response.request && response.to) {
var request_ids = [];
for (var i = 0; i < response.to.length; i++) {
var fb_id = response.to[i];
request_ids.push(fb_id);
//// here I want to get the user's names and
surnames so that I can send them along with the id's..
$.ajax({
url: '/Groups/SaveInvitedFriends',
type: 'POST',
data: { invitedFriends: request_ids }
});
}
}
}
Is there a way maybe in which the user's are saved upon accepting the request?
有没有办法在接受请求后保存用户?
Thanks
谢谢
回答by asifrc
You will need to make an API call to request the user's info. Facebook's JavaScript API docs gives the following example:
您将需要进行 API 调用以请求用户的信息。Facebook 的 JavaScript API 文档给出了以下示例:
FB.api('/me', function(response) {
alert('Your name is ' + response.name);
});
(from http://developers.facebook.com/docs/reference/javascript/#graphapi)
(来自http://developers.facebook.com/docs/reference/javascript/#graphapi)
me
refers to the currently logged in user. If you use a userid instead, the response
object will contain name
(full name), first_name
, last_name
(surname), and several other basic fields. For example, zuck
is the userid of Mark Zuckerberg, so using the request FB.api("/zuck", function(response) { console.log(response); });
returns the following:
me
指当前登录的用户。如果您改用用户 ID,该response
对象将包含name
(全名)、first_name
、last_name
(姓氏)和其他几个基本字段。例如zuck
是马克扎克伯格的userid,所以使用请求FB.api("/zuck", function(response) { console.log(response); });
返回如下:
Object {
first_name: "Mark"
gender: "male"
id: "4"
last_name: "Zuckerberg"
link: "https://www.facebook.com/zuck"
locale: "en_US"
name: "Mark Zuckerberg"
updated_time: "2013-05-09T05:12:23+0000"
username: "zuck"
}
Let me know if this makes sense or if you have any questions :)
让我知道这是否有意义或您有任何疑问:)