调用CSDN的开发API——>博客接口——>获取博主基本信息【2】
2017-05-17 07:48
681 查看
昨天试了以下CSDN的开放平台,通过API获取博客的系统分类:,参考地址:调用CSDN的开发API——>博客接口——>获取博客系统分类【1】 这个例子不需要用户复杂的验证,但是接下来的这个获取博主 的基本信息的API调用,会是一个比较又意思的事情,那我们就一边参考API,一边说下我整个操作过程吧。
http://open.csdn.net/wiki/api/blog/getinfo
http://open.csdn.net/wiki/oauth2
项目目录结构如下:(该项目我也分享到了这个地址:感兴趣可以去下载:链接:http://pan.baidu.com/s/1nuGofxf 密码:8eqv
编写工具类:GetDocumentByUrl.java,该工具类给定一个url,访问url获得返回信息的。
编写:HelloWorld.java类,页面显示获取Authorization Code的链接。
根据Authorization Code来获取Access Token,然后根据Access Token,获取用户信息
获取用户信息
获取博客基本信息的API
参考的获取博客基本信息的API网址:http://open.csdn.net/wiki/api/blog/getinfo
登录授权
仔细查看第一步的API会发现,这个是需要登录验证的,关于登录授权,CSDN也给出了相应的的API地址:http://open.csdn.net/wiki/oauth2
关于access_token
从第一步的API也发现,在访问请求时是需要必填access_token这个参数的,而这个参数的获得就必须依赖登录验证的过程。所以接下来,我们会在第二步给出的API的基础上来获取access_token这个参数的值(注:access_token这个值每次访问都会变动,切记不可同一个access_token使用多次)。两种验证授权机制
验证授权机制又两种分别是:Web应用的验证授权(Authorization Code)和客户端的验证授权(Resource Owner Password Credentials)【注:客户端的验证授权审核比较严格】,所以这里我使用的是web应用来获取验证授权的access_token的。Web应用实例操作
新建web项目项目目录结构如下:(该项目我也分享到了这个地址:感兴趣可以去下载:链接:http://pan.baidu.com/s/1nuGofxf 密码:8eqv
编写工具类:GetDocumentByUrl.java,该工具类给定一个url,访问url获得返回信息的。
package com.burns.servlet.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class GetDocumentByUrl { public static String getStringByUrl(String url) throws Exception { // 链接URL URL url1 = new URL(url); // 返回结果集 StringBuffer document = new StringBuffer(); // 创建链接 URLConnection conn = url1.openConnection(); // 读取返回结果集 BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream(), "utf-8")); String line = null; while ((line = reader.readLine()) != null) { document.append(line); } reader.close(); return document.toString(); } }
编写:HelloWorld.java类,页面显示获取Authorization Code的链接。
package com.burns.servlet; //导入必需的 java 库 import java.io.*; import java.net.URL; import java.net.URLConnection; import javax.servlet.*; import javax.servlet.http.*; //扩展 HttpServlet 类 public class HelloWorld extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private String message; public void init() throws ServletException { // 执行必需的初始化 message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); String urlStr = "http://api.csdn.net/oauth2/authorize?client_id=1100309&redirect_uri=http://localhost:8080/ServletTest/GetCode&response_type=code"; // String urlStr = "http://api.csdn.net/oauth2/access_token?client_id=1100309&client_secret=23eaf6637b2d43128c518b8d7e4078ac&grant_type=authorization_code &redirect_uri=http://localhost:8080/zckf/login.html&code=52cc18a67e5e4382a1a0c242cf45c307"; // 链接URL // URL url = new URL(urlStr); // 返回结果集 // StringBuffer document = new StringBuffer(); // 创建链接 // URLConnection conn = url.openConnection(); // // 读取返回结果集 // BufferedReader reader = new BufferedReader(new InputStreamReader( // conn.getInputStream(), "utf-8")); // String line = null; // while ((line = reader.readLine()) != null) { // document.append(line); // } // 设置响应内容类型 response.setHeader("content-type","text/html;charset=UTF-8"); // 实际的逻辑是在这里 PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); out.println("<a href='"+urlStr+"'>getcode</a>"); } public void destroy() { // 什么也不做 } }
根据Authorization Code来获取Access Token,然后根据Access Token,获取用户信息
package com.burns.servlet; //导入必需的 java 库 import java.io.*; import java.net.URL; import java.net.URLConnection; import javax.servlet.*; import javax.servlet.http.*; import com.burns.servlet.util.GetDocumentByUrl; import net.sf.json.JSONObject; //扩展 HttpServlet 类 public class GetCode extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private String message; public void init() throws ServletException { // 执行必需的初始化 message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); // String code = (String) request.getAttribute("code"); String code = request.getParameter("code"); String url = "http://api.csdn.net/oauth2/access_token?client_id=1100309&client_secret=23eaf6637b2d43128c518b8d7e4078ac&grant_type=authorization_code&redirect_uri=http://localhost:8080/ServletTest/get_access_token&code=" + code; // ------------------------------- // 返回结果集 String document = null; try { document = GetDocumentByUrl.getStringByUrl(url); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("document>>>" + document); JSONObject json = JSONObject.fromObject(document.toString()); // 查看json格式 // System.out.println(json.toString()); // 获取json中某个对象 // JSONObject obj = (JSONObject) json.get("weatherinfo"); // System.out.println(obj.toString()); // 获取对象中某个属性的值 String access_token = json.getString("access_token"); System.out.println("access_token>>>" + access_token); // ------------------------------- //获取用户信息 String url2 = "http://api.csdn.net/blog/getinfo?access_token="+access_token; String userInfo = null; try { userInfo = GetDocumentByUrl.getStringByUrl(url2); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("userInfo>>>"+userInfo); // 设置响应内容类型 response.setHeader("content-type","text/html;charset=UTF-8"); // 实际的逻辑是在这里 PrintWriter out = response.getWriter(); out.println("<h1>" + userInfo + "</h1>"); // out.println("<a href='" + urls + "'>get_access_token</a>"); } public void destroy() { // 什么也不做 } }
项目运行截图如下:
获取Authorization Code的链接获取用户信息
相关文章推荐
- 调用CSDN的开发API——>博客接口——>获取博客系统分类【1】
- C#微信公众号开发-高级接口-之网页授权oauth2.0获取用户基本信息(二)
- 微信公众平台开发 无高级接口账号获取用户基本信息
- 夺命雷公狗---微信开发52----网页授权(oauth2.0)获取用户基本信息接口(2)
- java微信开发API第三步 微信获取以及保存接口调用凭证
- 微信公众平台开发 无高级接口账号获取用户基本信息
- 微信公众平台开发(76) 无高级接口账号获取用户基本信息
- 微信公众平台开发(76) 无高级接口账号获取用户基本信息
- zabbix 二次开发之调用api接口获取历史数据
- 微信公众平台开发(76) 无高级接口账号获取用户基本信息
- 夺命雷公狗---微信开发51----网页授权(oauth2.0)获取用户基本信息接口(1)
- 夺命雷公狗---微信开发53----网页授权(oauth2.0)获取用户基本信息接口(3)实现世界留言版
- 夺命雷公狗---微信开发44----获取用户基本信息接口(1)
- 微信公众平台开发 无高级接口账号获取用户基本信息(转)
- 微信接口开发之高级篇系列【网页授权获取用户基本信息】
- java微信开发API解析(三)-高级功能的前奏----获取以及保存接口调用凭证
- 微信公众号开发接口列表大全之根据code获取用户基本信息
- 夺命雷公狗---微信开发45----获取用户基本信息接口(2)
- 微信公众平台开发(76) 无高级接口账号获取用户基本信息
- 微信公众平台开发(76) 无高级接口账号获取用户基本信息