回调地址配置:
回调地址很重要,要和自己请求时的回调地址一致。QQ对于回调地址只是匹配域名,其他几个是直接匹配整个地址,然后加上参数code进行回调。
1.发起授权请求,返回code值
页面链接:<a href="https://www.douban.com/service/auth2/auth?client_id=Config.APPID&response_type=code&redirect_uri=Config.CALLBACK_URL"></a>
2.回调方法
通过code值得到access_token 等信息。
获取授权用户信息。
进行业务操作。
主要http工具类
HttpUtil.java
- /**
- * @param url 访问地址
- * @param header 设置传递参数 Header header = new BasicHeader("access_token",********));
- * @param charset 编码,推荐“UTF-8”
- * @param pretty 是否美化,推荐true
- */
-
- public static String getMethedHeader(String url, Header header,String charset, boolean pretty) {
- StringBuffer response = new StringBuffer();
- HttpClient client = new HttpClient();
- HttpMethod method = new GetMethod(url);
- try {
- method.setRequestHeader(header);
- client.executeMethod(method);
- if (method.getStatusCode() == HttpStatus.SC_OK) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
- String line;
- while ((line = reader.readLine()) != null) {
- if (pretty)
- response.append(line).append(System.getProperty("line.separator"));
else - response.append(line);
- }
- reader.close();
- }
- } catch (URIException e) {
- log.error("执行HTTP Get请求时,编码查询字符串“" + url + "”发生异常!", e);
- } catch (IOException e) {
- log.error("执行HTTP Get请求" + url + "时,发生异常!", e);
- } finally {
- method.releaseConnection();
- }
- return response.toString();
- }
推荐博客:http://blog.csdn.net/wjl_mgqs/article/details/9450065