Link facebook connect accounts to user accounts on my site
I'm having some trouble wor开发者_Python百科king out where I need to start with facebook connect.
Basically I've set up my application in facebook and got the connect button working. Now I want to do something very similar to diggs implementation where upon logging into facebook they are directed to a create account page where they have to enter a username and one or two other details.
Do I need to capture the cookies returned, check my database to see if they exist and manually do this or is there a step I am missing?
No, using cookies is not a recommended process. I don't know ASP.net, but the following process works for me in other languages:
- Include the Facebook Javascript SDK in your HTML as indicated in the docs
- Add a Page Load call to FB.getLoginStatus similar to the example included here and handle the responses in the following way:
connected
means the user has registered for your site before and auth'd the associated FB app. In this case theresponse.authResponse
object will contain an access token that you should update in your database for this user, do this via an AJAX call to your server side code.not_authorized
means the user hasn't authorized the FB app associated with your site. In this case, you should use some Javascript to make the "connect" button appear (it should be invisible until this point, I think).- In any other case, the user isn't logged into Facebook, so just make the "connect" button appear anyway.
- Attach a call to FB.login to your "connect" button. As you can see in the documentation for that method, it will popup a Permission dialog which the user must accept in order to register for your app, and therefore your site. Whenever they accept, your Javascript code will receive the same type of
response.authResponse
object as mentioned above. In this case, you'll want to do something different with the access token. The following is just a vague idea of what you might want to do, but your approach should be broadly similar. - In the response function of FB.login above, wherever a successful outcome is determined, insert some additional Javascript which displays or creates the HTML of your sites registration form. It's probably best if you don't redirect away from the current page, so that you can keep most of this code client-side (otherwise you'll need to temporarily store the access token somewhere, etc.)
- Allow the user to input their registration details into this form, and once they submit, pass the access token and Facebook User ID you retrieved from the authResponse above along with the form fields.
- Process this with your server side code and store the access token and FB UID alongside the newly registered user details.
精彩评论