用户工具

站点工具


分享:技术:微博:微博用户信息获取整理

这是本文档旧的修订版!


微博用户信息获取整理

前提

  • 准备微博账号:cinvestors_app@tebon.com.cn
  • 登录微博开发平台,http://open.weibo.com/,添加中国好投资应用
  • 后台查看应用的App Key和App Secret

用户信息获取

获取code

get请求(页面跳转)

注意:

  1. 微博H5已经登录,当前浏览器保留登录cookie,否则跳到微博登录页面
  2. client\_id为App Key
  3. redirect\_uri为授权回调地址,需要做url转码,微博开发平台后端可以修改,如下图:

https://api.weibo.com/oauth2/authorize?client_id=1570065866&response_type=code&redirect_uri=https%3A%2F%2Fapi.weibo.com%2Foauth2%2Fdefault.html

请求成功,会跳转到redirect\_uri同时带上code

https://api.weibo.com/oauth2/default.html?code=d47ad7eeaf07901b9372645b3777a05c

获取access_token和uid

根据code做post请求(可以从服务端请求)

注意:

  1. client\_id为App Key
  2. client\_secret为App Secret
  3. code为上一步获取的code
  4. redirect\_uri为授权回调地址,不做url转码,微博开发平台后端可以修改,如下图:
HttpClientUtils.java
String url = "https://api.weibo.com/oauth2/access_token";
Map<String, String> mapData = new HashMap<String, String>();
mapData.put("client_id", "1570065866");
mapData.put("client_secret", "388725da138272e2d577fbcf480816ea");
mapData.put("grant_type", "authorization_code");
mapData.put("redirect_uri", "https://api.weibo.com/oauth2/default.html");
mapData.put("code", "d47ad7eeaf07901b9372645b3777a05c");
String content = HttpClientUtils.getWebContentByPost(url, mapData, "GBK");
System.out.println("content=" + content);

返回json,服务端解析得到access\_token和uid

{"access_token":"2.00eaYP_GArpPiB597d053aa6S3d7FD","remind_in":"157679999","expires_in":157679999,"uid":"5781255264"}

参考帖子

分享/技术/微博/微博用户信息获取整理.1459307243.txt.gz · 最后更改: 2016/03/30 11:07 由 gxx