Use java to develop facebook website applications

Source: Internet
Author: User

When you want to write a Facebook app in Java? You downloaded the java class library from the program development module added to your facebook account. Suddenly you find that you have to stop: development-related introduction, where are the sample code and documentation instructions? Such frustration is so angry that you have to give up.

I hope someone will write a good Getting Started Guide for developing facebook applications using Java as soon as possible. Maybe you just need to give me some delicious chocolate. We can write some related introduction documents. To be honest, we have solved many related problems. Here are some tips to help you use java to start building your own facebook app. This is not a complete Getting Started Guide, but it can help you avoid detours when you start.

Eight considerations help you build Java/facebook applications

1. For your happiness and health, we recommend that you use JDK 5.0 or higher.

2. Don't look at the columns in the facebook java Development Kit, because those examples are for java Desktop applications. Generally, you need to write a web application. The difference between the two applications is still great. In the beginning, you need a web application service container. You can select tomcat, but I won't. Here is another story. I will find a proper time to tell you. Facebook's network applications have an uncommon call mode. Most of your pages will be called by facebook. When a user requests a facebook page, the main content in the page will be returned to the user from your server. Your Integrated content will be integrated into facebook pages and returned to users. In most cases, these processes can be executed without your intervention. However, you must note that the javascript on the page generated on your server cannot be executed, which means that common ajax is no longer used in facebook. If you need to use AJAX technology to implement the functionality that facebook provides that mock-ajax cannot do, you need to jump to the general page on your service catalog through the facebook page.

3. Configure your facebook application

Do not forget to fill in the configuration file of the facebook application. You should set the callback URL of the application.

Application name-this name is used to mark the page on facebook. (These pages can be embedded into facebook's navigation pages with iframe, or pages marked with FBML ). Once set, the page like http://apps.facebook.com/yourappname/yourpagename will generate a request from facebok to your server. For new users, this is a welcome page for your application. Invite users to spread your application.

4. Use facebookrestclient

Facebookrestclient is the most important class in the client class library. This class contains a series of methods to call the facebook server and covers most of the features you need. Unfortunately, facebookrestclient is not a class with clear architecture. Almost all requests require a facebookrestclient which is used to build a session key. After logging in, you can obtain the session key from the CGI variable. (Refer to FacebookParam. SESSION_KEY.toString (). If you have not logged on to the console, you must go to a logon page. Try the following methods:

// Create a FacebookRestClient without a session

FacebookRestClient client = new FacebookRestClient (your api key, your encryption key );

String token = client. authcreateToken ();

String loginURL = "http: // www.facebook.com/login.php? V = 1.0 & apikey = & "+ your apikey +" & auth_token = "+ token;

// Go to the login page

// The session key will be returned after login

FacebookRestClient. auth_getSession () is useless in my experience. Its function is to convert a client without a session into a client with a session. I found getting sessions from CGI variables is easier than trying this method.

When there is a FacebookRestClient with a session key, you can call various facebook editing methods provided by this class. These methods can be easily exchanged with facebook. However, unfortunately, these methods return unprocessed XML documents, which will be troublesome for later processing.

For example, friends_get () returns

<Document>

<Friendsgetresponse>

<Uid> 1 </uid>

<Uid> 2 </uid>

<Uid> 3 </uid>

</Friendsgetresponse>

</Document>

You may want to encapsulate some methods to extract information, such as calling friends_get (). Try this

Document d = client. friends_get ();

NodeList userIDNodes = d. getElementsByTagName ("uid ");

Int fcount = ids. getLength ();

List <Integer> friends = new ArrayList <Integer> ();

For (int I = 0; I <fcount; I ++)

{Node node = userIDNodes. item (I );

String idText = node. getTextContent ();

Integer id = Integer. valueOf (idText );

Friends. add (id );}

In this way, the user id is filled in the friends list.

5. The servlet returns a partial page.

When you create a page on facebook, such as an address example.

6. Learn FBML.

FBML is a unique set of facebook tags. Trial tags allow you to easily complete many functions (such as displaying user names and images ).

7. Invite friends

Do you want to invite friends to complete viral promotion? Facebook recently modified his api (2007.11 ). The new version mainly improves a special form, fb: form. For more information, see http://wiki.developers.facebook.com/index.php/fb:request-form.

FacebookRestClient. icationications_sendrequest () is no longer in use. If you call this function accidentally, an exception is returned.

8. User File Information Display page

To add some information to the user's archive information page, use FacebookRestClient. profile_setFBML (). This method rewrites the default information page you set in the application. When you set the FBML pages of your profile and these pages are under traffic, facebook will cache the page information without immediate updates. In this way, dynamic updates of user information will be troublesome. When an event triggers user information changes, you need to call profile_setFBML () again to update the information. Fortunately, since you already have a login client that can do anything, this client can also set any data items for your user data.

Okay, that's all. I am a beginner in facebookapi. If you are an expert, please leave a comment.

We wish you a smooth journey towards building Web.

Author: Daniel Translator: kwyjibo

Original article: Tips for writing FaceBook applications in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.