Java Web site Integration Sina Micro Blog login

Source: Internet
Author: User
Tags config oauth split java web

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.