微信接口获取用户信息

必须的参数:appid和appSecret
 
①获取code
 
接口链接示例:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbafc7fdf3664b600&redirect_uri=http%3a%2f%2f0708.pinzhi365.com%2findex_tb.html&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect

参数说明:
appid                服务号自带的值,为固定值
redirect_uri       回调的链接,在经过接口获取到了code之后,页面会跳转到此参数对应的链接上,且域名一定要对应服务号所配置的域              名
                          且该链接必须要经过urlencode
response_type  固定值
scope                固定值:snsapi_userinfo或snsapi_base(不弹出授权页面,只能获取用户openid)
state                  重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
 
调用结果:
如果用户同意授权,页面将跳转至

http://0708.pinzhi365.com/index_tb.html?code=011bdbf34b5748203155ef831321b9af&state=STATE

------------------------------------------------------------------------------------------------------------
②通过code换取网页access_token
 
接口链接示例:

https://api.weixin.qq.com/sns/oauth2/access_token?

 

appid=wxbafc7fdf3664b600&secret=81f9832f79e858b247aa6a2d09d66341&code=011bdbf34b5748203155ef831321b9af&grant_type=authorization_code

 
参数说明:
appid                 同①
secret                 服务号自带,即appSecret,为固定值
code                  从①获取的值
grant_type         固定值
 
调用结果:
示例:
{
    "access_token": "OezXcEiiBSKSxW0eoylIeByO7M0QcUqqAnU8rzQAkrRdZmwxk9aQoDf5WT5aSvx6wBenI2Jmz_jBs-025_piaJ81bvZrb9Q1oEPOPZq7RMQRZT2sBN3-mk6siHEeXIN9WD1ybSIfoHaITDowh1AN8Q",
    "expires_in": 7200,
    "refresh_token": "OezXcEiiBSKSxW0eoylIeByO7M0QcUqqAnU8rzQAkrRdZmwxk9aQoDf5WT5aSvx695Vnx345AXlPPDt50Fbl0zfUI5eTGht57oMtwLx_aMyjYF3Juj4AVA_VX0hltk10WL47Z3_v6BnKCD_o9v2S5Q",
    "openid": "oAhzJs2lQRR6Dd1U95S1lrlMKjSE",
    "scope": "snsapi_userinfo",
    "unionid": "o_LdHt0WTwyrk41R2vcAs2v5XFQk"
}
------------------------------------------------------------------------------------------------------------
③通过access_token和openid获取用户信息
 
接口链接示例:
 

https://api.weixin.qq.com/sns/userinfo?access_token=OezXcEiiBSKSxW0eoylIeByO7M0QcUqqAnU8rzQAkrRdZmwxk9aQoDf5WT5aSvx6wBenI2Jmz_jBs-025_piaJ81bvZrb9Q1oEPOPZq7RMQRZT2sBN3-mk6siHEeXIN9WD1ybSIfoHaITDowh1AN8Q&openid=oAhzJs2lQRR6Dd1U95S1lrlMKjSE&lang=zh_CN

 
参数说明:
access_token      由②所获取
openid                由②所获取
lang                    语言类型,为固定值

调用结果:
示例:
{
    "openid": "oAhzJs2lQRR6Dd1U95S1lrlMKjSE",
    "nickname": "雨天发呆",
    "sex": 1,
    "language": "zh_CN",
    "city": "",
    "province": "",
    "country": "中国",
    "headimgurl": 

"http://wx.qlogo.cn/mmopen/TTQibyKjrickytMz9ZYicsibWECY84XyItRFZMp5kYv7vVyNxGMCkl1x1L6eOzGIT2ZGhApDx3n0jDfMy3aodO8PXQ/0",

    "privilege": [],
    "unionid": "o_LdHt0WTwyrk41R2vcAs2v5XFQk"
}
------------------------------------------------------------------------------------------------------------
附:
JAVA发送http get请求的方法:
 
所必须的jar包:commons-httpclient-3.0.1.jar
 
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
 
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public static String sendGet(String url) {
  String newStr = "";
  // 构造HttpClient的实例
  HttpClient httpClient = new HttpClient();
  // 设置 Http 连接超时为3秒
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
  // 创建GET方法实例
  GetMethod getMethod = new GetMethod(url);
  // 设置 get 请求超时为 3秒
  getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 3000);
  // 使用系统提供的默认的恢复策略
  getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
    new DefaultHttpMethodRetryHandler());
  try {
   // 执行getMethod
   int statusCode = httpClient.executeMethod(getMethod);
   if (statusCode != HttpStatus.SC_OK) {
    System.err.println("Method failed: " + getMethod.getStatusLine());
   }
   // 读取内容
   newStr = new String(getMethod.getResponseBodyAsString().getBytes("ISO8859-1"),"UTF-8").trim();
  } catch (HttpException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   // 释放连接
   getMethod.releaseConnection();
  }
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
  System.out.println(newStr);
  return newStr;
  }
 
利用json-lib-2.3-jdk15.jar来解析json的方法
String openIdJson = "
{
    "access_token": "OezXcEiiBSKSxW0eoylIeByO7M0QcUqqAnU8rzQAkrRdZmwxk9aQoDf5WT5aSvx6wBenI2Jmz_jBs-025_piaJ81bvZrb9Q1oEPOPZq7RMQRZT2sBN3-mk6siHEeXIN9WD1ybSIfoHaITDowh1AN8Q",
    "expires_in": 7200,
    "refresh_token": "OezXcEiiBSKSxW0eoylIeByO7M0QcUqqAnU8rzQAkrRdZmwxk9aQoDf5WT5aSvx695Vnx345AXlPPDt50Fbl0zfUI5eTGht57oMtwLx_aMyjYF3Juj4AVA_VX0hltk10WL47Z3_v6BnKCD_o9v2S5Q",
    "openid": "oAhzJs2lQRR6Dd1U95S1lrlMKjSE",
    "scope": "snsapi_userinfo",
    "unionid": "o_LdHt0WTwyrk41R2vcAs2v5XFQk"
}
";
 
JSONObject jo = JSONObject.fromObject(openIdJson);
String openId = jsonObjectOpenId.getString("openid");
 
 

 

微信接口获取用户信息

上一篇:微信开放平台获取不到UnionID的解决办法


下一篇:微信分销系统,您可以使用它?