Sina Weibo • Open platform: http://open.weibo.com/
I want to write an application needs Sina Weibo user authentication, made a variety of errors 1 days, reference to a large number of online interpretation finally succeeded,
Experience to write down and share with you.
First, create your own application:
(Figure 1)
(Figure 2)
(Figure 3)
(Figure 4) fill in the relevant information: Java Desktop Project selection: client-desktop. Note that the application address here is your future application of the download address, if there is no can write http://www.baidu.com, such as access to the address can be, and so have a formal address can be modified. Click "Create"
(Figure 5) (Figure 6)
By this you have created your application and acquired the key and URI through steps 7th and 9th.
Second, OAuth2.0 certification
1, OAuth2.0 overview
Most of the access to the API, such as microblogging, access to DMS, attention to the need for user identity, the current Sina Weibo open platform user authentication has OAuth2.0 and basic Auth (only for the application of the developer Debugging Interface), the new version of the interface to support only these two ways. OAuth2.0 is simpler and more secure than 1.0 for the entire authorization verification process, and is the most important way to authenticate and authorize users in the future.
About the OAUTH2.0 protocol authorization process to view the OAuth2.0 authorization process, where the client refers to the Third-party application, Resource owner refers to the user, Authorization server is our authorized server, Resource Server is an API server.
Developers can first browse the OAuth2.0 interface document, familiar with the OAuth2 interface and the meaning of parameters, and then according to the application scenario to explain how to use OAuth2.0.
| Interface |
Description |
| Oauth2/authorize |
Requesting user Authorization Token |
| Oauth2/access_token |
Get an authorized access Token |
| Oauth2/get_token_info |
Authorization Information Query interface |
| Oauth2/revokeoauth2 |
Authorization Recycle interface |
| Oauth2/get_oauth2_token |
OAuth1.0 access Token Replace to OAuth2.0 access Token |
1. OAuth2 Verification Steps
Step One: Modify the configuration information for the config.properties. Open this file and fill in your information as follows:
client_id = (you apply for the Appkey see Figure 5)
Client_sercret = (you apply for the application of the App_secret)
Redirect_uri =https://api.weibo.com/oauth2/authorize (see Figure 6 and "Authorization callback page:" Address same)
BaseURL = https://api.weibo.com/2/
Accesstokenurl = Https://api.weibo.com/2/oauth2/access_token
Authorizeurl = Https://api.weibo.com/2/oauth2/authorize
The second step opens the file in the example directory Oauth4code.java,code the following figure
Step three: Run this file, the browser will automatically open a Web page, as shown below. Enter your account number and password in it, click "Login and Authorize" If you do not see the micro-blog authentication interface or interface error code is "Redirect_uri_mismatch 21322" The reason for the error is that the Redirect_uri address in config.properties and the inconsistency in Figure 6 result in a consistent change.
Fourth: Click on the authorization, the browser will automatically jump to the callback page (because the callback page will not be authenticated, so the browser will open a Third-party licensing error interface, in the following figure), the address bar in the "Https://api.weibo.com/oauth2/authorize?" code=##### "in the ##### copy out.
Fifth step: Fill out xxxxx to the Eclipse's console [Enter] below, and then enter. Finally get a string of information Accesstoken [accesstoken=2.00fee9xc5ilvkea41d4c9090xhhi2e, expirein=157679999, refreshtoken=,uid= 2710758337]
Step Sixth: Fill in the Java run configurations->arguments with the information you get, note two parameters: accesstoken=2.00fee9xc5ilvkea41d4c9090xhhi2e, uid= 2710758337 spaces in the middle
Seventh step: To this you are a successful certification, you can try to use the Java project in other examples test function, to note that because the project is not audited, so the validity of the authorization is 1 days, that is, every day to repeat the OAuth once.