微信公众号开发(3)——获取access_token

接上面几讲内容:

微信公众号开发(1)——服务器配置

微信公众号开发(2)——文本消息、图文消息发送

 

获取Access_token的微信公众平台文档:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183

要先将自己的ip放入ip白名单中,

参考文章:

IP白名单添加了当前IP,获取access_token时依然报出错误码40164的坑

注意在浏览器中输入URL,URL中替换掉自己的APPID和APPSECRET,直接访问也可测试是否获取access_token

代码测试如下:

AccessToken类:

 

package com.imooc.po;

public class AccessToken {

	private String token;
	private int expiresIn;
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	public int getExpiresIn() {
		return expiresIn;
	}
	public void setExpiresIn(int expiresIn) {
		this.expiresIn = expiresIn;
	}
	
}

 

package com.imooc.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.imooc.po.AccessToken;

import net.sf.json.JSONObject;

public class WeixinUtil {

	private static final String APPID="wxfc73577ca8460519";
	private static final String APPSECRET="b0b75d65ccb882f8b5526538b48121cb";
	private static final String ACCESS_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
	
	/**
	 * get请求
	 * @param url
	 * @return
	 */
	public static JSONObject doGetStr(String url) {
		DefaultHttpClient httpClient=new DefaultHttpClient();
		HttpGet httpGet=new HttpGet(url);
		JSONObject jsonObject=null;
		
		try {
			HttpResponse response=httpClient.execute(httpGet);
			HttpEntity entity=response.getEntity();
			if(entity!=null) {
				String result=EntityUtils.toString(entity,"utf-8");
				jsonObject=JSONObject.fromObject(result);
				
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	
	/**
	 * post请求
	 * @param url
	 * @param outStr
	 * @return
	 */
	public static JSONObject doPostStr(String url,String outStr){
		DefaultHttpClient httpClient=new DefaultHttpClient();
		HttpPost httpPost=new HttpPost(url);
		JSONObject jsonObject=null;
		
		try {
			httpPost.setEntity(new StringEntity(outStr,"utf-8"));
			HttpResponse response=httpClient.execute(httpPost);
			String result=EntityUtils.toString(response.getEntity(),"utf-8");
			jsonObject=JSONObject.fromObject(result);
		}catch (Exception e) {
				e.printStackTrace();
			}
		
		
		return jsonObject;
	}
	
	
	/**
	 * 
	 * 获取access_token
	 * @return
	 */
	public static AccessToken getAccessToken() {
		AccessToken token=new AccessToken();
		String url=ACCESS_TOKEN_URL.replace("APPID",APPID ).replace("APPSECRET",APPSECRET);
		JSONObject jsonObject= doGetStr(url);
		if(jsonObject!=null) {
			if(jsonObject.containsKey("access_token")) {	
				token.setToken(jsonObject.getString("access_token"));
				token.setExpiresIn(jsonObject.getInt("expires_in"));
			}else {
				System.out.println("获取access_token失败");
			}
		}
		return token;
	}
}

  测试是否获取到微信服务器的AccessToken:

package com.imooc.test;


import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {

	public static void main(String[] args) {
		
	AccessToken token=WeixinUtil.getAccessToken();
	System.out.println("票据:"+token.getToken());
	System.out.println("有效时间:"+token.getExpiresIn());
	}
}

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员资料站

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值