Now many sites have integrated convenient third-party login, such as QQ login, Sina Weibo, Sohu, NetEase and so on, to provide users with a lot of convenience and save time. We can choose to use JS or SDK to implement a third-party user authorization API, this article mainly on the JAVA SDK Sina Weibo login authorization and access to user information.
Note: This example uses SPRINGMVC, so if you want to copy the code directly, you need to build the environment in advance.
1, first apply for Sina Weibo website access: http://open.weibo.com/wiki/
2. Become a developer to get app key and app Secret
3. Download Weibo4j-oauth2:http://code.google.com/p/weibo4j/downloads/list
4, write to get user information code, done!
After downloading the extract, copy the Weibo4j folder and config.properties in the SRC directory to the SRC directory of your JAVA WEB project.
Edit the config.properties configuration file and change it into your own data. The first to fill in the app you apply key value, the second to fill in the app secret value.
123 |
client_id = Client_sercret =redirect_uri = |
The third Redirect_uri is your redirected URL. should correspond to a controller (Spring controller) or Servlet. Here we need two URLs, the first URL is the user click on the page URL, can jump to the Sina Weibo account login interface, when the user clicks the login button of the account login interface, Sina Weibo will verify the correctness of the data, once the data is correct will be to get Mode request and carry the parameter code redirect to the Redirect_uri that you fill in, this time can obtain the user's data.
Take Xiao Ming's blog As an example, first the user clicks Http://www.bugxm.com/user/login-sian-weibo.html, we receive the request after the main work is redirected to the Sina Weibo login page. The code snippet is as follows:
12345678910111213141516 |
/** * * Sina Weibo Login interface * * @param session * @return * @throws WeiboException */ @RequestMapping (value = "/user/ Login-sian-weibo.html ", method = requestmethod.get) public string loginsinaweibo (httpservletrequest request, httpsession session) throws WeiboException { session.setattribute ("Login_current_url", request.getheader ("Referer")); weibo4j. Oauth oauth = new weibo4j. Oauth (); string&Nbsp;url = oauth.authorize ("Code", ""); return "REDIRECT:"  + URL;    } |
The session in the code snippet is to get the URL of the page before the user logs in, so it's easy to redirect to the page before signing in. Where the URL is the Sina Weibo login interface URL, we just need to redirect the past on the line.
When the user on Sina Weibo login successful, will redirect to the Redirect_uri we just filled in, such as I fill in the http://www.bugxm.com/user/login-sina-weibo.do, corresponding logic snippet is as follows:
1234567891011121314151617181920 |
/** * * Sina Weibo login operation * * @param session * @return * @throws WeiboException */ @RequestMapping (value = "/user/login-sina-weibo.do ", method = requestmethod.get) public String Loginsinaweiboaction (httpsession session, @RequestParam string code) throws WeiboException { String url = (String) session.getattribute ("Login_current_ URL "), session.removeattribute (" Login_current_url "); user User = userservice.sinaweibologinaction (code, (user) session.getattribute ("user"); if (Null != user) { session.setattribute ("User", user); } return "Redirect:" + url; } |
Redirect comes with the code parameter we take the value to get the data for the user. In this code, except code, sinaweibologinaction () are secondary, for this topic is optional. Let's take a look at the sinaweibologinaction () code snippet:
12345678910111213141516171819202122232425262728293031323334 |
/** * Sina Micro-blog Operation * * @param code * @param currentLoginUser * currently logged in user, available for null * @return * @throws weiboexception */ public user Sinaweibologinaction (String code, user currentloginuser) throws WeiboException { weibo4j. Oauth oauth = new weibo4j. Oauth (); weibo4j.http.accesstoken accesstoken = Oauth.getaccesstokenbycode (code); string uid&Nbsp;= accesstoken.getuseruid (); weibo4j. Users users = new weibo4j. Users (); users.client.settoken (Accesstoken.getaccesstoken ()); weibo4j.model.user user = users.showuserbyid ( UID); string userdomain = User.getuserdomain (); // User login name string username = user.getscreenname (); // user called string avatar = user.getavatarlarge (); // User Portrait String gender = null; // Gender if ("M". Equals (User.getgender ())) { gender = "Male"; } else if ("F". Equals ( User.getgender ())) { gender = "Female"; } else { gender = "Unknown"; } // ...... business logic operations, code slightly return currentloginuser; } |
where string uid = Accesstoken.getuseruid (), the Getuseruid () method itself is not, we need to modify the Sina Weibo source code, or take the UID is very troublesome.
Edit/src/weibo4j.http/accesstoken.java, in:
123 |
Public String Getrefreshtoken () {return refreshtoken;} |
Add the following:
123 |
Public String Getuseruid () {return UID;} |
We're done here. The user's other information has been commented in detail in the/src/weibo4j/model/user.java.
PHP Integrated Micro-Blog login