To use the Sina Weibo open platform API, you should first get the app key and app Secret that Sina allocates, and the following is the app key and app Secret that Sina assigns after I created the app (this is confidential.)
Then download the microblogging SDK, I use Java weibo4j. There are currently the following language versions:
Modify the Weibo.java class's app key and app Secret in the SDK pack for the app key and app Secret just acquired, as illustrated in the following illustration:
After you have done this, you can start writing code based on the demo provided. As follows:
Weboauth.java, which is used to initialize the app Key and app Secret required for the Weibo.java class, and provides methods for obtaining request Token and Access Token Getrequesttoken (), Gettaccesstoken () with the required parameters as shown in the code. In addition, a method of publishing a text microblog update () is also provided.
Package weibo4j.examples;
Import weibo4j. Status;
Import weibo4j. Weibo;
Import weibo4j. Weiboexception;
Import Weibo4j.http.AccessToken;
Import Weibo4j.http.RequestToken;
Import java.io.UnsupportedEncodingException;
Web Mode Authentication
public class Weboauth {
Private Weibo Weibo;
Public Weboauth () {
Get ready for Consumer Key, Consumer Secret.
Corresponding to Sina Weibo application is applied to the app key and Secret key
system.setproperty ("Weibo4j.oauth.consumerKey", Weibo.consumer_key);
system.setproperty ("Weibo4j.oauth.consumerSecret", Weibo.consumer_secret);
Weibo = new Weibo ();
}
Obtain the request token according to the incoming Callback_url
Public Requesttoken Getrequesttoken (String backurl) {
try {
Specify Callback_url and GET request token
Requesttoken Requesttoken = Weibo.getoauthrequesttoken (Backurl);
System.out.println ("Request token:" + requesttoken.gettoken ());
System.out.println ("Request token secret:" + Requesttoken.gettokensecret ());
return requesttoken;
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Get request token exception. ");
E.printstacktrace ();
return null;
}
}
Get access token based on the incoming request token and verifier
Public Accesstoken Gettaccesstoken (Requesttoken requesttoken, String verifier) {
try {
Accesstoken Accesstoken = Weibo.getoauthaccesstoken (requesttoken
. GetToken (), Requesttoken.gettokensecret (), verifier);
System.out.println ("Access token:" + accesstoken.gettoken ());
System.out.println ("Access token secret:" + Accesstoken.gettokensecret ());
return accesstoken;
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Get access token exception. ");
E.printstacktrace ();
return null;
}
}
Publish microblogging based on incoming Access Token and content
public void Update (Accesstoken access, String content) {
try {
Weibo.settoken (Access.gettoken (), Access.gettokensecret ());
Content = new String (content.getbytes ("GBK"), "UTF-8");
Status status = Weibo.updatestatus (content);
System.out.println ("Published Weibo successfully:" + status.gettext () + ".");
catch (Unsupportedencodingexception e) {
SYSTEM.OUT.PRINTLN ("Micro-blog content transcoding occurs abnormally.") ");
E.printstacktrace ();
catch (Weiboexception e) {
System.out.println ("The publication of Weibo has been unusual. ");
E.printstacktrace ();
}
}
}
request.jsp, used to provide callback_ URL (we customize this here to the callback.jsp below), after obtaining the Requesttoken, save the Requesttoken into session and redirect the page to callback.jsp for authentication, authorization.
<%@ page contenttype= "Text/html;charset=utf-8"%>
<%@ page language= "java" import= "weibo4j.*"%>
<%@ page language= "java" import= "weibo4j.http.*"%>
<%@ page language= "java" import= "weibo4j.util.*"%>
<jsp:usebean id= "Weboauth" scope= "session" class= "Weibo4j.examples.WebOAuth"/>
<%
if ("1". Equals (Request.getparameter ("opt"))
{
Incoming Callback_url
String Callback_url = "http://localhost:8080/sinaweibo/callback.jsp";
Requesttoken Requesttoken = Weboauth.getrequesttoken (Callback_url);
if (Requesttoken!= null) {
Out.println (Requesttoken.gettoken ());
Out.println (Requesttoken.gettokensecret ());
Session.setattribute ("Requesttoken", Requesttoken);
String URL = requesttoken.getauthorizationurl () + "&oauth_callback=" +callback_url;
System.out.println ("Authorizationurl:" + URL);
Barebonesbrowserlaunch.openurl (Callback_url);
Response.sendredirect (Requesttoken.getauthorizationurl ());
Redirect to the Sina Weibo authentication page with the Callback_url callback address attached
Response.sendredirect (URL);
}else{
OUT.PRINTLN ("request Error");
}
}else{
%>
<a href= "Request.jsp?opt=1" > Please click on the OAuth certification for Web mode. </a>
<%}%>
callback.jsp, after the redirect in the previous step, the Callback_url is appended with the Oauth_verifier parameter, which we then base on the Requesttoken that is saved in the session and the Oauth_ Verifier parameter request to get Accesstoken. Once we get the Accesstoken, we redirect the page to the writeweibo.html page where we write the microblog.
<%@ page contenttype= "Text/html;charset=utf-8"%>
<%@ page language= "java" import= "weibo4j.http.*"%>
<%@ page language= "java" import= "weibo4j.*"%>
<jsp:usebean id= "Weboauth" scope= "session" class= "Weibo4j.examples.WebOAuth"/>
<%
Get the Oauth_verifier parameter in the HTTP request
String verifier=request.getparameter ("Oauth_verifier");
Out.println ("Oauth_verifier:" +verifier);
System.out.println ("Oauth_verifier:" +verifier);
if (verifier!= null) {
Requesttoken Requesttoken = (requesttoken) session.getattribute ("Requesttoken");
if (Requesttoken!= null) {
Accesstoken Accesstoken = Weboauth.gettaccesstoken (requesttoken,verifier);
if (Accesstoken!= null) {
try{
Session.setattribute ("Accesstoken", Accesstoken);
Out.println ("Turn to writeweibo.html in 5 Seconds");
Thread.Sleep (5000);
Response.sendredirect ("http://localhost:8080/sinaweibo/writeWeibo.html");
}catch (Exception e) {
E.printstacktrace ();
}
}else{
OUT.PRINTLN ("Access token request Error");
}
}else{
OUT.PRINTLN ("Request token session error");
}
}else{
Out.println ("Verifier String error");
}
%>
writeweibo.html, very simple HTML file.
<body bgcolor= "#d0d0d0" >
<form action= "updateweibo.jsp" method= "POST" >
Please write the text within 140 characters here:</br>
<textarea name= "Weibotext" rows= "3" cols= "a" > Test Sina Weibo. </textarea></br>
<input type= "Submit" value= "Release" >
<input type= "Reset" value= "clear" ></br>
</form>
</body>
<%@ page contenttype= "Text/html;charset=utf-8"%>
<%@ page language= "java" import= "weibo4j.http.*"%>
<%@ page language= "java" import= "weibo4j.*"%>
<jsp:usebean id= "Weboauth" scope= "session" class= "Weibo4j.examples.WebOAuth"/>
<%
Accesstoken Accesstoken = (accesstoken) session.getattribute ("Accesstoken");
String weibotext = (string) request.getparameter ("Weibotext");
Continuous publication of the same Weibo content will return 400 errors
Weboauth.update (Accesstoken, Weibotext);
Out.println ("Weibo published successfully.") ");
%>
Before running, we have to prepare Tomcat and put the above source files in the correct directory. In addition, you should add the Commons-httpclient-3.1.jar package in the SDK package to the \web-inf\lib directory, as well as my own compiled, packaged Weibo4j.jar (which is a concrete Java implementation in the Sina Weibo open platform).
Run Tomcat and access the request.jsp page in the browser, as shown in the following figure:
Click on the link, as shown below (note changes to the Address bar):
The URL of the address bar is as follows:
Http://api.t.sina.com.cn/oauth/authorize?oauth_token=efda6f2499877d0e6d814f8c3d31a1d1&oauth_callback=http ://localhost:8080/sinaweibo/callback.jsp
Fill in the specific valid Sina Weibo account number, password and authorization. Here are the results of the Weibo account that I tested and authorized:
The URL of the address bar is as follows:
Http://localhost:8080/sinaweibo/writeWeibo.html
Click "Publish", as shown below:
Log in to the micro-blog to view the following figure:
Check out the list of applications authorized by this account:
This is probably the process of using the Sina Weibo open platform to publish Weibo on the OAuth approach.
Summary:
1, in fact, there are a lot of details can not be mentioned, I have tried many times just a little bit to find the problem, understand the problem, then to solve the problem;
2, if the browser has saved our login Sina Weibo account information cookies, then the authorization does not enter the account information, of course, you can modify the current account without authorization;
3, there are some information entered by the console, such as token, URL, server return information is not given a screenshot.