Sina Weibo authorization and sending
To develop Sina Weibo, you must first use the official APIWeibo4android. Jar
: Http://download.csdn.net/source/3399114
Http://code.google.com/p/weibo4j/downloads/list
First, introduce the official API jar package of Sina Weibo in the project.
:
Recently, a friend said that the reason why the key he applied for cannot be used is that Xauth is required to apply for the key, because my key is provided by the company, so it can be used because the Xauth application conditions have been met. Please take a closer look at the official content below.
Conditions of application: http://open.weibo.com/wiki/XAuth
Xauth application requirements
Because Xauth still obtains the user's plaintext password, Xauth is limited to open. We recommend that you use the Desktop client to use oauth at the initial stage of development, and the mobile client use WAP 1.0 or 2.0 page authorization. Use Xauth to improve user experience when the number of users reaches a certain scale.
Xauth application conditions:
Applications belong to desktop clients and mobile clients.
The application itself has passed the open platform text, square review, and displayed on the square for more than 15 days.
The number of applications is more than 3000.
The functions of the application are closely related to Sina Weibo.
Any developer must apply for the consumerkey and consumersecret of Sina Weibo.
Application address: http://open.weibo.com/devel.php
Click "create application" and follow the prompts to create an application step by step. I will not describe it in detail here.
Please read the following code carefully
// The consumerkey and consumersecret must apply for system by yourself. setproperty ("weibo4j. oauth. consumerkey "," XXXX "); system. setproperty ("weibo4j. oauth. consumersecret "," XXXX "); Weibo = new Weibo (); string userid =" XXXX "; // Sina Weibo account string Password =" XXXX "; // try {Weibo. getxauthaccesstoken (userid, password, "client_auth"); Weibo. updatestatus ("posting a Sina Weibo");} catch (weiboexception e) {e. printstacktrace (); If (E. getstatuscode () = 400) {// The content is repeated. Sina Weibo does not allow repeated content publishing. if the content is repeated, an exception will be thrown here} else if (E. getstatuscode () = 403) {// incorrect account password} return ;}
Account Password authorization only once
In this way, the authorization succeeds and a meager message is sent, but this is not perfect because if the same account is used to send a meager message each time, there is no need to grant the authorization once and then store the token and tokensecret. after sending the Token, you only need to read the token and tokensecret to send the token directly.
Published successfully
Store token and tokensecret
You only need to authorize the account and password once.
Try {accesstoken accesen en = Weibo. getxauthaccesstoke (userid, password, "client_auth"); string token = accesstoken. gettoken (); string tokensecret = accesstoken. gettokensecret (); // here the token and tokensecret can be obtained to store it in shared preferences, and then Weibo can be used each time it is obtained independently. updatestatus ("Saving token and tokensecret to shared Preferences");} catch (weiboexception e ){}
ReadToken and tokensecret
Read the previously authorized key
Try {// token and tokensecret can get the previously saved content in shared preferences string token = NULL; string tokensecret = NULL; Weibo. setoauthaccesstoken (token, tokensecret); // you do not need to obtain the accesen en through the account password every time. updatestatus ("Get token and tokensecret in shared Preferences");} catch (weiboexception e ){}
Weiboexception
Weiboexception occurs when we use the API interface. For example
Incorrect account password: E. getstatuscode () = 403
Repeated sending: E. getstatuscode () = 400
Wait, if the code is implemented, we can throws these exceptions to the upper layer at the underlying layer to handle these exceptions.
The following link is the API error code provided by sina. You can read it carefully as needed.
Http://open.weibo.com/wiki/index.php/Help/error