获取数据的两种方式 记录一下

/*获取到用户的openid和access_token*/
public static String getWechatOpenId(String code, String appid, String secret)
{
  /**
   * grant_type:获取access_token填写client_credential
   * appid :商户的唯一标识
   * secert :第三方用户唯一凭证密钥
   */
  String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
  /**请求返回的数据解析封装到一个对象中去*/
  String json = HttpUtils.doGet(url);
  oAuthToken jsonToPojo = (oAuthToken)JsonUtils.jsonToPojo(json, oAuthToken.class);
  json = jsonToPojo.getOpenid();
  return json;//返回openis
}
/**
 * 获取access_token
 * @param appid
 * @param secret
 * @return
 */
public static String getWechatAccess_Token(String appid, String secret)
{
  /**请求的地址*/
  String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
  /**通过get的方式去获取这个*/
  return HttpUtils.doGet(url);
}
/**
 * 获取 access_token
 * method2: 通过加载配置文件中的配置
 * @return
 */
public static String getWechatAccess_Token()
{
  String json = null;
  try {
    /**load current package's 属性文件*/
    InputStream in = new WechatUtils().getClass().getResourceAsStream("/app.properties");
    /**创建一个属性文件*/
    Properties props = new Properties();
    /**创建一个输入流 把读取到的数据输出到流中*/
    InputStreamReader inputStreamReader = new InputStreamReader(in, "UTF-8");
    props.load(inputStreamReader); /**属性文件加载流*/
    String appid = props.getProperty("appid");/**获得我们需要的数据*/
    String secret = props.getProperty("secret");/**获取我们需要的数据*/
    /**定义一个url地址 send Get request 过去 带上读取出来的数据*/
    String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
    json = HttpUtils.doGet(url);
  }
  catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }
  catch (IOException e) {
    e.printStackTrace();
  }
  return json;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值