Java calls Sina Weibo API interface tweet, article by article, absolutely clear

Source: Internet
Author: User



recently to do a curriculum design, the use of micro-bo control Raspberry Pi, Raspberry Pi control LED light and off, the main design is divided into two layers, the upper layer is the use of Java call Sina Weibo API to monitor the micro-bo, when my microblog is back to automatically read the comments, The control action of the Raspberry Pi is determined according to the content of the comment instruction. The lower layer is to use the C language to invoke the operating system underlying interface to implement the Raspberry Pi Gpio interface to control the light emitting diode flashing, light off.
because the online tutorials are very old, the recent microblogging interface has changed a lot, so I decided to write a new detailed blog post, if you can help, it will be my great honor. At the same time I very much hope to communicate with you, have questions please reply me in the comments.
This article will explain how to apply for microblogging interface, step by step with Java to publish their own Weibo. "First you need a Weibo account"
?
first, apply to become a developer
http://open.weibo.com/to apply here. Click on "My App" to see the identity authentication: not certified, do what you do, not in detail here. The review was quick and passed less than a day.
then it should be like this (haha, welcome to the mutual powder ha):



Second, create the application
This online tutorial is misleading, I follow those tutorials, the results dragged me into the pit, I have climbed out, I hope you don't fall in.
If you want to use Java to control Weibo, you can create an app without creating an "on-site app," to create a "mobile app," and note that this is not the same as the permission verification step.
"micro-connect"--"mobile app"--"Access Now"--"continue to create"
Now we came to fill in the information, the application of their own name (the name of the late change will be very troublesome, please fill it once), such as "mobile phone control Raspberry Pi", the client selected "mobile phone", the application platform selected Android on the line, in fact, no impact. Then "create". Okay, now we're in a new interface, we're just testing it, so we don't have to review it unless you want your app to go live.


Third, important information settings and records
This step is very important, directly related to the future can not be properly connected.
left panel: "App info"--"Advanced info"
See "OAuth2.0 Authorization Settings" and click "Edit". "Authorization callback page:" and "Cancellation authorization callback page:" It is important that those who created the "in-station app" should be crying because they cannot modify the "Authorization callback page:" ...
So how do you change it? two are changed to: Https://api.weibo.com/oauth2/default.html, this is the default Weibo, we have enough testing.




then click "Basic Info",



Note: App Key and App Secret, later useful.


Iv. Download The Weibo Java SDK
come here, https://github.com/sunxiaowei2014/weibo4j-oauth2-beta3.1.1/.

Download ZIP archive package: Weibo4j-oauth2-beta3.1.1.zip


v. Import the SDK into Eclipse
What's that? Have you been writing Java with a text editor? OK, listen to my advice, write this thing or change to eclipse, or you will be exhausted.
open Eclipse, "file"--"Import"--"general under existing Projects into Workspace"--"Select archive File"--select just the SDK compression package--" Finish ".
The import is complete.


Vi. Modification of configuration information


Open the Config.properties file in the diagram and change the values after "client_id" and "Client_sercret" to the "app Key" and "app Secret" you noted. The value of "Redirect_uri" is changed to https://api.weibo.com/oauth2/default.html
saved.


VII. Application Authorization


compile and execute the Oauth4code.java file.
your browser will be automatically called to open an authorization page. such as:

Click "Authorize".

go to such as:

Don't worry about closing the browser! please copy the string that follows code= in the address bar.
back in Eclipse, the console below is waiting for you to enter something. Hit enter when it's done . [Enter]:
yes, that's him. Paste the string you just copied, enter.
OK, wait a few seconds for your authorization information to return.
Accesstoken [Accesstoken=xxxxxxxxxxxxxxxx, expirein=157679999, refreshtoken=,uid=xxxxxxxxx]
The string after "Access_token" and the number after "UID" are important, write it down.
Now you can close the browser.
Here's a reminder that if you were to create an app that was "in-app" instead of "mobile app," then this step would not be successful and you would always see the "Error:redirect_uri_mismatch" page.


Eight: Hair Weibo
Weibo SDK package inside, you see examples there are many programs, these are examples, covering everything that the SDK can do, you just have to refer to the write.
Weibo4j.examples.timeline The Updatestatus.java in this package is an example of a tweet.
we set up a new package called Myweibo in SRC, and then set up several new classes below, and here I have built several.


I'll show you how to tweet.
Create a new class under Myweibo Sendweibo.java
The Main method body in the Examples--weibo4j.examples.timeline--updatestatus.java example program is copied all over, and after pasting, Eclipse automatically imports the package that needs to be referenced.
Replace the string access_token= with the value of the access_token you just made. String statuses= is followed by your Weibo text.
after the change is done:

then run the Java code on the line, as long as the error will be successful. Take a look at your microblog.


Note: If you don't know a function (I'm used to calling a function, not calling a method) what it is, don't know its parameter meaning, move the cursor over the function.




the various functions provided in the SDK are very easy to use, what to get Weibo, get comments, get Comment ID, reply to comments, get geographical location, get photos, send photos, get fan relations, get friends Circle Weibo and so on, too many, we slowly try it.


Nine: Publish the program:
If you're going to run the program somewhere else, like Raspberry Pi Linux or Linux on a virtual machine, there are some places to be aware of.
Let's publish the program first:
we weibo4j-oauth2 this project by right-clicking on "Export" and then

Next,

which program is the entrance, Launch configuration on which to choose, here we are the implementation of the microblog program: Sendweibo.
Choose the path, the following library handing choose a second insurance point. Then finish, may pop up warning, no tube, click OK.
and then it's generated. Then you double-click the jar package, the program flashed over, leaving a Weibo.log file, which is a log. In case of an error, you can view the log for reasons. Now that the program is executed, it has released a microblog.
You can drag this file to the virtual machine Linux, we see how Java is running around, the result Java-jar 1.jar after the error, how to do it?
Take a closer look at the error message, which says Java.net.UnknownHostException, and then see if the error message says Inetaddress.getlocalhost () is the way it is, yes it does.
Solution:
First ifconfig look at your IP, this does not care about the LAN IP or the external network IP, only requires that you see the native IP on the line.
Modify/etc/hosts This host file, add a row,
Your IP     your host name   # Kd.localdomain is my hostname

As for how to view the host name, in the error message should have, such as Localhost.localdomain. Kd.localdomain This is my own change. You can also check the/etc/hosts to confirm your hostname.
After this modification, run 1.jar again, it should be successful. I just can't. It's a hassle to drive a virtual machine.
PS: After the microblogging test on the line, do not hair, this effect is similar to the brush screen, will be beaten.


Call API This thing is sometimes very easy to go wrong, welcome everyone to comment on the exchange. I am learning Linux kernel bottom, make Java is not very proficient, is a dabbler level, if swim, also please do not laughed at. You are also welcome to communicate with me about Linux issues.



Java calls Sina Weibo API interface tweet, article by article, absolutely clear

Related Article

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.