如何從第三方百度賬號登錄
前提條件:要有自己的服務器和域名(不需要經過備案)。
一,先在百度開發者中心上註冊自己的賬號
二,鼠標放在右上角自己賬戶名稱上,選擇進入應用管理界面,
三,創建工程:創建成功後點擊基本信息,會給出API Key 和Secret Key。
四,在eclipse裏新建項目venu,在項目裏面新建兩個jsp文件,名字分別爲index.jsp和result.jsp,並新建Servlet文件,文件名爲: BaiDuServlet,其中文件名可以自己隨意命名。
代碼如下.
將第三部中的API Key後面的值複製,粘貼在String client_id後面對應的" "裏面 ;將第三部中的Secret Key後面的值複製,粘貼在String client_secret後面對應的" "裏面;String redirect_uri後面 asdasd.top爲自己的域名,再後面爲自己在eclipse中創建的工程名和Servlet文件名。(下面是我隨意寫的值)
String redirect_uri = "http://www.asdasd.top/venu/BaiDuServlet";
String client_secret = "vssASX5Y85BbLSXozG8is6NQEr2LMF0";
String client_id = "dasDDxTXmIovXqFcbtuT3lc";
下面爲BaiDuServlet代碼:
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.sadweq.top/venu/BaiDuServlet";
String client_secret = "asdasdjzxxK4ntIBj7i9w9tfIAwmCOwGA";
String client_id = "asdasdl7whO1XfEVStrjMUpeS";
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);
}
}
index.jsp裏面代碼如下:
注意 client_id爲API Key值,redirect_uri爲自己的新建的BaiduServlet文件名
<%@ 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=Y2sIzl7whO1XfEVStrjMUpeS&redirect_uri=http://www.shadowtercel.top/venu/BaiDuServlet&display=popup";
}
</script>
<body>
<input type="button" value="百度登錄" οnclick="bdlogin()">
</body>
</html>
result.jsp裏面代碼如下:
<%@ 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>
<body>
<%=request.getAttribute("message") %>
</body>
</html>
五,在項目文件做好之後,導出到war包裏
六,再在開發者中心裏,打開安全設置,
根域名綁定填寫自己的域名,應用服務器ip地址和服務器IP
七,用自己的服務器,將工程發佈,
八,發佈之後,在瀏覽器中打開,點擊百度登錄
登陸之後便會在網頁返回個人信息
注:關於第三方登錄所需要的jar包彙總下載