getting self info from facebook api
i want to retrieve my own personal info from my facebook account to display on my website. i do not want the viewer to be prerequisitely logged into the facebook env. i dont want to ask the visitor to authenticate any apps. I want to do this using hte Javascript api. any pointers?
is there a possible way 开发者_如何学Cto accomplish this?
Sure. First, you'll need to set up a Facebook App for your domain. Then you'll need to grant the app permission to access your profile. This is typically done using the FBML
<fb:login-button params="some permission" />
or JS SDK
FB.login(callback, { perms:'some permission'});
Granting your app will be a one-time situation so you can just write a temporary page to accomplish that. Afterwards, on your actual public-facing pages, you'll need to initialize the JS SDK with FB.init()
and then query the FB API:
FB.api('/path-to-your-facebook-page', callback)
Where '/path-to-your-facebook-page'
is the URL of your facebook page minus the domain and callback
is the function that is exceuted when the API returns. If your profile is http://www.facebook.com/amit
, you would call FB.api('/amit', callback);
From there, the callback
function will expose a JSON object with all of the information that you authorized your app to access.
You may want to consider using a server-side SDK to query the API. It will limit the amount of your information that the makes it to the browser. For server-side SDKs, you should check out the official PHP SDK or the C# SDK. There are also official iOS and Android SDKs if you are going mobile.
Accessing the graph API requires authentication: either a user auth token or an app auth token. If you do not want the user to have to authorize your app you might be able to create your Facebook app, ask the user to enter their Facebook vanity name or Facebook id (if they know it) and then perform an app login on the server to retrieve the user's graph API endpoint.
Will be messy: it feels like they have intentionally made it difficult to do without an app, and I seriously doubt it can be done from the JavaScript API without an app.
精彩评论