Third party login - Baidu

Posted by sureshmaharana on Fri, 03 Jan 2020 12:23:03 +0100

Third party login - Baidu

Log in from a third-party baidu account

Prerequisite: have its own server, domain name.

First register an account in Baidu Developer Center

Then enter the application management interface under the account,

Create a project, and the API Key and Secret Key will be given after the project is created successfully.

Then create a new project venu in eclipse, index.jsp and result.jsp in the project, and create a new Servlet file BaiDuServlet

The code is as follows

Note that string client ID is API Key, client secret is Secret Key

        String redirect_uri = "http://www.aiyibiao.top/Venu/BaiDuServlet";
        String client_secret = "vlhIXxX5Y85BbLSXozG8is6NQEr2LMF0";
        String client_id = "ttSzZpxTXmIovXqFcbtuT3lc";

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class BaiDuServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String redirect_uri = "http://www.aiyibiao.top/Venu/BaiDuServlet";
		String client_secret = "vlhIXxX5Y85BbLSXozG8is6NQEr2LMF0";
		String client_id = "ttSzZpxTXmIovXqFcbtuT3lc";
		String url1 = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=" + code
				+ "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "";

		String content1 = "";

		try {
			
			CloseableHttpClient httpClient = HttpClients.createDefault();
			
			HttpGet getReq = new HttpGet(url1);

			getReq.addHeader("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8    ");
			getReq.addHeader("Accept-Encoding", "gzip, deflate, sdch, br");
			getReq.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
			getReq.addHeader("Cache-Control", "max-age=0");
			getReq.addHeader("Connection", "keep-alive");
			getReq.addHeader("Host", "openapi.baidu.com");
			getReq.addHeader("User-Agent",
					"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");

			HttpResponse res = httpClient.execute(getReq);

			HttpEntity entity = res.getEntity();
			content1 = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> map = JSON.parseObject(content1, new TypeReference<Map<String, Object>>() {
		});
		String access_token = (String) map.get("access_token");

		print(access_token, request, response);
	}

	public void print(String access_token,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String content = "";
		String url = "https://openapi.baidu.com/rest/2.0/passport/users/getInfo?access_token=" + access_token + "";
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet getReq = new HttpGet(url);

			getReq.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8    ");
			getReq.addHeader("Accept-Encoding", "gzip, deflate, sdch, br");
			getReq.addHeader("Accept-Language", "zh-CN,zh;q=0.8");
			getReq.addHeader("Cache-Control", "max-age=0");
			getReq.addHeader("Connection", "keep-alive");
			getReq.addHeader("Host", "openapi.baidu.com");
			getReq.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");

			HttpEntity entity = httpClient.execute(getReq).getEntity();
			content = EntityUtils.toString(entity, "UTF-8");
			System.out.println(content);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map<String, Object> map = JSON.parseObject(content, new TypeReference<Map<String, Object>>() {});
		System.out.println(map);
		String baiduid = (String) map.get("userid");
		System.out.println(baiduid);
		request.setAttribute("message", map);
		request.getRequestDispatcher("/result.jsp").forward(request, response);
	}
}

The code in index is as follows:

Note that the client? ID is the API Key value, and the redirect? URI is the new Baidu servlet file name

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
        function bdlogin(){
            location.href="https://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=ttSzZpxTXmIovXqFcbtuT3lc&redirect_uri=http://www.aiyibiao.top/Venu/BaiDuServlet&display=popup";
        }
</script>
<body>
<input type="button" value="Baidu login" onclick="bdlogin()">
</body>
</html>

The result.jsp code is as follows

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%=request.getAttribute("message") %>
</body>
</html>

After the project file is completed, it is exported to Jar package

In the developer center, open the security settings,

The root domain name is bound to fill in its own domain name, and the application server IP address is filled in the server IP address

Open the server again and publish the project,

Open in browser after publishing

Click Baidu to log in

Return personal information after login

The summary is as follows

About the summary and download of jar package required by the third party login

https://download.csdn.net/download/monster_ayb/10669765

 

 

 

 

 

 

 

 

 

 

 

 

 

Topics: Apache JSP Java xml