Java based on OAUTH2.0 Sina Weibo authorized authentication, Sina Weibo login, data crawl, view, send micro bo, etc.

Source: Internet
Author: User
Tags oauth
Original: Silence is gold qq:79284467 Sina microblog nickname: Bus

Effect Chart:


OAUTH2.0-based Web projects in the Java language

Introduction: OAUTHA2.0: Sina Weibo open platform, providing many interfaces Api,java based on OAUTH2.0 Sina Micro-Blog Authorization certification, Sina Weibo login, data crawl, view, Weibo, etc.

Function: 1. Third-party login, that is, Sina Weibo account login, after landing can get the relevant information of account

2. Data crawl, Sina Weibo account login, access to such as the latest microblogging, the hottest microblogging, my microblog and other data, the data from the Sina server to crawl to their own web projects, can be stored in their own local database steps: 1. Build Java,web frame 2.http ://open.weibo.com/Sina Weibo open platform, download the Java version of the SDK, where packages and classes are replicated within a Web project, introducing jar packages

3. Registered Sina Weibo test application:

1 "Sina open platform home--" micro-link-"Create Application-" Web application

2 "Fill in the name, address-" create

3 "Fill in more details-" Write down appkey,Appsecret, submit

4 "Click Application Management--" advanced information, edit the authorization callback page (for Sina Weibo login, authorized after the jump page address, here with a servlet address, that is, Sina Weibo login, successful authorization, and then jump a servlet, Servlet jump page to Web application home page, for example:http://127.0.0.1:8080/project/redirectServlet

5 "myeclipse--" Web Project-"config.properties--" modified client_id for Appkey, modify Client_sercret for Appsecret, modify Redirect_uri for authorization callback page, Other unchanged













4. Obtain Access_token, authorize login authentication

1> the servlet class that configures the Web's login button response, you can tune the servlet address directly in the browser to test

Package com.wy.webiter;

Import java.io.IOException;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import weibo4j. Oauth;
Import weibo4j.model.WeiboException;
Import Weibo4j.util.BareBonesBrowserLaunch;

public class Oauth4code extends HttpServlet {
	/**
	 * Blog Login
	 /
	private static final long Serialversionuid = 1L;

	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);
		}
	}

	public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
		doget (request, response);
	
}


2 The Servlet class that configures the web's authorization to successfully callback, the servlet address for creating the application's authorization callback page, and the Sina account's successful callback to the servlet address, the servlet's information for the authorized Sina user, etc.


Package com.wy.webiter;
Import java.io.IOException;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession; Import weibo4j.
Oauth; Import weibo4j.
Users;
Import Weibo4j.model.User;
Import weibo4j.model.WeiboException;
Import Com.wy.dao.ConsumerDao;
Import Com.wy.form.ConsumerForm;
Import Com.wy.tool.Chinese; public class Redirectservlet extends HttpServlet {/** * callback servlet/private static final long Serialversionuid = 1
	L public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {Strin
		G code = Request.getparameter ("code");
			try {Oauth Oauth = new Oauth ();
			String token = oauth.getaccesstokenbycode (code). toString ();
			SYSTEM.OUT.PRINTLN (token); 
			String str[] = Token.split (","); 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);
			Users um = new users ();
			Um.client.setToken (Accesstoken);			
			User user = Um.showuserbyid (UID);						
The user's Get method can obtain its properties, the specific properties can be pressed CTRL click the user class, enter the user class to view System.out.println ("hehe:" + user.tostring ());
RequestDispatcher RequestDispatcher = Request.getrequestdispatcher ("Your JSP page. jsp");
Requestdispatcher.forward (request, response);
catch (Weiboexception e) {e.printstacktrace ();}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {doGe

T (request, response); }
}


/**
* After fetching the data from the user object, you can put it in the request object and upload it to the page (it can also be passed in JSON, Web Basics),
* Data can also be stored in the database via JDBC
* In short, the data to get, how to toss it all by you
* In the SDK package, there is a example package, which has a lot of instance APIs, specific examples of the role, please check the Sina open Platform website API,
* Open an instance, such as the Showuser class to modify the response parameter, the UID to the user object's Uid property, Accesstoken to get the Accesstoken
* These values can be stored in the session for easy fetching
* Right Key run javaapplication, the console can see through the Sina server access to data
* The API instance needs to be copied directly into a newly created servlet class (similar to the servlet authorization above) to convert an instance to a Web project **/








5. The application of the Test level can only use the developer's Sina Weibo login authorization certification, can manually add 15 test account 1 "Login Sina Weibo open platform, access to management applications 2" Click My application into the specific information 3 "test information, right edit, add nickname 4" The user who is added right now has access to your application and is authorized to authenticate.



2014--5--13 Silence is Golden

qq:79284467








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.