Write a Java program that calls the Sina Weibo API to send a microblog _java

Source: Internet
Author: User

First of all, need to download the SDK of Sina Weibo, here is attached address: http://vdisk.weibo.com/s/z7iFc2gCCwC1b

After downloading, unpack, then open MyEclipse, create a new project, and then extract the import to the project just now. As shown in the figure:

Next, go to this URL http://open.weibo.com/registration application. There are three applications, select site applications, and then create applications. Fill in all the fill. Make sure it's OK. There are two points to be aware of:

1, is registered application, will have the app key and app Secret, this next will use.

2, to "My Application", select: Application information,


Click "Edit", and then note that there are two addresses: "Application of the actual address" to be carefully filled out, my is http://127.0.0.1/callback.jsp, pay attention to the address behind the "/" these. Now, remember this address, and then use it.

Now, turn to MYECLIPSE,SRC, have config.properties file, edit it

CLIENT_ID is the app key.

Client_sercret is the app Secret

Redirect_uri is the address previously highlighted: http://127.0.0.1/callback.jsp

Save.

Next, we send a tweet:

Under the examples, under the Weibo4j.examples.oauth2 package: Oauth4code class, direct operation: will appear the application authorization page, enters the user name and the password, will jump to before fills in the Redirect_uri page. If you have information stored in your browser, skip the page and jump directly to the Redirect_uri page.

After jumping to the Redirect_uri page, see the URL address bar, the format is Redirect_uri? Code=xxxxxx. Below, copy the value of the code, and then go to the console output of MyEclipse and see:

Code=https://api.weibo.com/oauth2/authorize?client_id=1458508643&redirect_uri=http://127.0.0.1/ Callback.jsp&response_type=code
Hit Enter when it ' s done.[ Enter]:

Next, paste the code to [Enter]: back. Enter. You will see a lot of output information.

Jump straight to the end, see Access_token, this is what we need, recorded.

Next send Weibo:

Under the Weibo4j.examples.timeline package, under the UpdateStatus class/

Need to pass two parameters to go in, I just write directly in. The code is as follows:

Packageweibo4j.examples.timeline; Importweibo4j. 

Timeline; Importweibo4j. 

Weibo; 

Importweibo4j.examples.oauth2.Log; 

Importweibo4j.model.Status; 


Importweibo4j.model.WeiboException; Publicclassupdatestatus {publicstaticvoidmain (string[] args) {String Access_token = "2.00LBVA1CXIKHABFBC0D2A0C10FWT 

TI "; 

String statuses = "This micro-blog from the Starlight release System release"; 

Weibo Weibo = Newweibo (); 

Weibo.settoken (Access_token); 

Timeline TM = Newtimeline (); try{Status Status = TM. 

UpdateStatus (statuses); 

Log.loginfo (Status.tostring ()); 

catch (Weiboexception e) {e.printstacktrace ();

}} package Weibo4j.examples.timeline; Import weibo4j.
Timeline; Import weibo4j.
Weibo;
Import Weibo4j.examples.oauth2.Log;
Import Weibo4j.model.Status;

Import weibo4j.model.WeiboException; public class UpdateStatus {public static void main (string[] args) {String Access_token = "2.00LBVA1CXIKHABFBC0D2A0C10
 Fwtti ";
 String statuses = "This micro-blog from the Starlight release System release";
 Weibo Weibo = new Weibo (); Weibo.settoken (ACCess_token);
 Timeline TM = new Timeline (); try {Status Status = TM.
  UpdateStatus (statuses);
 Log.loginfo (Status.tostring ());
 catch (Weiboexception e) {e.printstacktrace ();

 } }

}

Statuses is what you want to publish.

Here, you can send microblogging.

There is a problem message.

Report:

Sometimes, for example, when you use a different Twitter account (which is different from registering the application), there are some errors. Error message:

{"Error": "Applications over the unaudited use restrictions!", "Error_code": 21321, "request": "/2/statuses/update.json" } eibo4j.model.weiboexception:403:the The request is understood, but it has been refused. 

An accompanying error message would explain why. Error:applications over the unaudited use restrictions! Error_code:21321/2/statuses/update.json at Weibo4j.http.HttpClient.httpRequest (httpclient.java:414) at Weibo4j.http.HttpClient.httpRequest (httpclient.java:372) at Weibo4j.http.HttpClient.post (httpclient.java:301) at Weibo4j.http.HttpClient.post (httpclient.java:286) at weibo4j. Timeline.updatestatus (timeline.java:708) at Weibo4j.examples.timeline.UpdateStatus.main (updatestatus.java:18) {"

Error ":" Applications over the unaudited use restrictions! "," Error_code ": 21321," request ":"/2/statuses/update.json "} Weibo4j.model.weiboexception:403:the request is understood, but it has been refused.
 An accompanying error message would explain why. Error:applications over the UnaudiTed use restrictions!. Error_code:21321/2/statuses/update.json at Weibo4j.http.HttpClient.httpRequest (httpclient.java:414) at Weibo4j.http.HttpClient.httpRequest (httpclient.java:372) at Weibo4j.http.HttpClient.post (httpclient.java:301) at Weibo4j.http.HttpClient.post (httpclient.java:286) at weibo4j.

 Timeline.updatestatus (timeline.java:708) at Weibo4j.examples.timeline.UpdateStatus.main (updatestatus.java:18)

This solution: or in my application there, click the "Application Information", "Test Account", see the addition of test account, this time, you want to release the account number of Weibo user nickname added in, OK.

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.