To add a Third-party login to the site function, the study of the next Sina Weibo login interface, would have wanted to use it to provide the kind of JS code library Way, the results have always been uncertain, because not proficient JS can only give up. Download a Java SDK package: weibo4j-oauth2-beta2.1.1, the research, although some problems, but also quickly integrated into the site.
First, take care of some of the necessary packages and Java classes to your own engineering files:
Modify the Client_id,client_sercret,redirect_uri inside the config file.
Write a Twitter login button on the login page and hyperlink to the servlet named Oauth4code.
Import weibo4j. Oauth;
Import weibo4j.model.WeiboException;
Import Weibo4j.util.BareBonesBrowserLaunch;
public class Oauth4code extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, ioexception{
Oauth Oauth = new Oauth ();
try{
Barebonesbrowserlaunch.openurl (Oauth.authorize ("Code", ""));
}catch (Weiboexception e) {
System.out.println (e);
}
}
After the login is successful, you will jump to the Redirect_uri in config file.
I am also redirecting a servlet with the core code as follows:
String code = request.getparameter ("code");
try{
Oauth Oauth = new Oauth ();
//
Accesstoken token = oauth.getaccesstokenbycode (code);
String token = oauth.getaccesstokenbycode (code). toString ();
SYSTEM.OUT.PRINTLN (token);
//
String Accesstoken = Token.getaccesstoken ();
String uid = Token.getuid ();
System.out.println (Accesstoken);
SYSTEM.OUT.PRINTLN (UID);
String str[] = Token.split (","); Intercepts strings, obtains Sccesstoken and UID
String accesstoken= str[0].split ("=") [1];
System.out.println (Accesstoken);
String str1[] = Str[3].split ("]");
String uid = str1[0].split ("=") [1];
SYSTEM.OUT.PRINTLN (UID);
Session.setattribute ("Accesstoken", Accesstoken);
Session.setattribute ("UID", UID);
Users um = new users ();
Um.client.setToken (Accesstoken);
User user = Um.showuserbyid (UID);
String screenname = User.getscreenname (); Get a nickname for a user
Session.setattribute ("name", screenname);
}catch (Weiboexception e) {
System.out.println (e);
}
Originally want to get the UID and token through the Accesstoken class, the result will appear the code error and so on phenomenon, can only hard own division. After successful acquisition of UID and token can be changed according to the instance code, I was to get the user nickname.