Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get Facebook user Information through Javascript Facebook API

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to obtain Facebook user information through Javascript Facebook API. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

It is needless to say that you need to be Facebook APP ID before using APP.

Initialize Facebook information in javascript after generating Facebook APP ID

And I've defined some global variables that I need to get the data.

/ / this is the information variable we want to get.

Var facebookId

Var facebookName

Var facebookProfileImg

Var facebookBooks

Var facebookMovies

Var facebookMusic

Var facebookLocations

Var friendFbIds =''

Var facebookEmail

FB.init ({

AppId: '138343092941586, / / the APP ID you applied for is set here

Status: true

Cookie: true

Xfbml: true

Oauth: true

})

Copy the code

After initialization is complete, the function of getting Facebook information can be triggered by an event. (I used the click event of id)

$('# invite'). Click (function () {

FB.getLoginStatus (checkResponse)

})

The / / checkResponse method is used to detect whether the current user has logged in.

Function checkResponse (response) {

If (! Response.authResponse) {

/ / scope means to get permission. You can get other permissions, such as offline_access, publish_stream, etc.

FB.login (handleSessionResponse, {scope:'email'})

} else {

/ / this method is the way to get Facebook user information.

RetrieveProfiles ()

}

}

/ / the method is used to deal with the response of Facebook login.

Function handleSessionResponse (response) {

If (! Response.authResponse) {

Return

} else {

/ / this method is the way to get Facebook user information.

RetrieveProfiles ()

}

}

Copy the code

Here is the key point. According to the Facebook information obtained above, the relevant information can be obtained by querying the Facebook database through FacebookAPI.

/ / it is used to cycle through Facebook friend ID.

Object.size = function (obj) {

Var size = 0

For (var key in obj) {

If (obj.hasOwnProperty (key)) size++

}

Return size

}

Function retrieveProfiles () {

/ / query user information through Facebook API

FB.api ({method: 'fql.query', query:' SELECT uid,email, name, pic_square, books, movies, music, current_location FROM user WHERE uid=me ()'}, function (response) {

/ / fetch the status from response

Var user = response [0]

If (user = = undefined) {

$('span.loading'). ReplaceWith ('Sorry, please try again.')

} else {

FacebookId = user.uid

FacebookName = user.name

FacebookProfileImg = user.pic_square

FacebookMovies = user.movies

FacebookMusic = user.music

FacebookBooks = user.books

FacebookEmail = user.email

FacebookLocations = user.current_location

/ / get the Facebook friends of the current user

FB.api ({method: 'fql.query', query:' SELECT uid2 FROM friend WHERE uid1 ='+ facebookId}, function (data) {

Console.log (data)

/ / Note that the Object.size method is used here

For (var I = 0; I < Object.size (data); iTunes +) {

FriendFbIds = friendFbIds + "'" + data [I] .uid2 + "'"

If (I! = Object.size (data)-1) {

FriendFbIds + =','; Renji Psychiatric treatment in Wuhan

}

}

Console.log (friendFbIds)

Console.log ("facebookName" + facebookName)

Console.log ("facebookEmail" + facebookEmail)

Console.log ("facebookProfileImg" + user.pic_square)

Console.log ("facebookMovies" + user.movies)

Console.log ("facebookMusic" + user.music)

Console.log ("facebookBooks" + user.books)

Console.log ("facebookLocations" + user.current_location)

})

}

})

}

The above is the editor for you to share how to obtain Facebook user information through Javascript Facebook API, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report