PS: the content has been updated, see: http://binary.duapp.com /? P = 69
1. Obtain the "pass" app key and app secret required for application development
First, you must have a Sina account. Open http://open.weibo.com/your account number.
Click"I am a developer":
Click "create application ":
Click "client ":
In this case, a page is displayed for entering developer information:
After entering the information and verifying the email address, go to the application creation page:
Click "Basic Information" under "application information" and findAppkeyAndAppsecret
Ii. Download the SDK
Next, go to http://open.weibo.com/wiki/sdkto obtain the required sdks. Java is used as an example:
You can download the required JDK directly at http://code.google.com/p/weibo4j.
Import the SDK to the Java project of the Development client you have created. Click the file "config. properties" as follows:
Enter:
3. A simple example of getting current user information and publishing a microblog:
Add method in accesstoken:
public String getUid() {return uid;}
The code for modifying oauth4code is as follows:
Package weibo4j. examples. oau22. import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import weibo4j. oauth; import weibo4j. timeline; import weibo4j. users; import weibo4j. weibo; import weibo4j. HTTP. accesstoken; import weibo4j. model. status; import weibo4j. model. user; import weibo4j. model. weiboexception; import weibo4j. util. barebonesbrowserlaunch; public class oauth4code {publicstatic void main (string [] ARGs) throws weiboexception, ioexception {oauthoauth = new oauth (); barebonesbrowserlaunch. openurl (oauth. authorize ("Code"); system. out. println (oauth. authorize ("Code"); system. out. print ("hitenter when it's done. [enter]: "); bufferedreaderbr = new bufferedreader (New inputstreamreader (system. in); stringcode = BR. readline (); log. loginfo ("Code:" + code); accesstokenaccesstoken = NULL; try {accesstoken = oauth. getaccesstokenbycode (CODE);} catch (weiboexception e) {If (401 = E. getstatuscode () {log. loginfo ("unableto get the access token. ");} else {e. printstacktrace () ;}} stringaccess_token = accesstoken. getaccesstoken (); weiboweibo = new Weibo (); Weibo. settoken (access_token); stringuid = accesstoken. getuid (); usersum = new users (); try {useruser = Um. showuserbyid (UID); // obtain the information of the current user system. out. println ("Current User name:" + User. getname (); system. out. println ("Number of Weibo posts by the current user:" + User. getstatusescount (); system. out. println ("current user follow:" + User. getfriendscount (); system. out. println ("current user fans:" + User. getfollowerscount ();} catch (weiboexception e) {e. printstacktrace ();} // test the message sent by Weibo timelinetm = new timeline (); system. out. print ("Enter the text of the microblog to be sent: [enter]:"); BR = new bufferedreader (New inputstreamreader (system. in); stringnewstatus = BR. readline (); TM. updatestatus (newstatus );}}
Click "run" to open a page for user authorization (PS: At first, many people may think that user authorization here refers to your application's authorization to the user. In fact, this is not the case.User-authorized applicationsTo allow your application to obtain the user's information ):
Note: The Sina account used here must be the account of the current application or the "test user" of your application. Each application that has not passed the application review can have 15 test users. You can add "test user" to "advanced information" of the application information ".
After entering the account and password, click authorize. Note that the following page appears (error page ...) :
We can see the code = in the address bar, which is exactly what we need. copy it, paste it on the console, and press enter to view some basic information about the current user.
For more details about how to obtain data, see the API documentation provided by Sina Development Platform: http://open.weibo.com/wiki/api?e6=96=87%e6%a1%a3_v2.
Finally, enter the Weibo text you want to send in the console and test the new Weibo text (the main code is as follows, which already exists in the oauth4code class above ):
Timeline TM = new timeline (); system. out. print ("Enter the text of the microblog to be sent: [enter]:"); BR = new bufferedreader (New inputstreamreader (system. in); string newstatus = BR. readline (); TM. updatestatus (newstatus );
Remember to press enter after entering the information, and you can see it on your Weibo homepage:
Iv. Questions
Here, I would like to ask you guys: is there a way not to use a browser,DirectWhich code does the Java program obtain? (This problem has been solved temporarily !!!)
PS: the content has been updated, see: http://binary.duapp.com /? P = 69