https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
到 https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=1470128375&lang=zh_CN
获取:AppID AppSecret
点击 启用
会跳出:
手机扫码确认
电脑端有要干什么?
当执行 /Weixin/src/com/imooc/test/WeixinTest.java
获取票据的时候,一定要把开发电脑的ip添加到白名单 ,不然就报错
/Weixin/src/com/imooc/po/AccessToken.java
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;
}
}
/Weixin/src/com/imooc/util/WeixinUtil.java
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 = "wx3041db7fcb232fd7";
private static final String APPSECRET = "b2e7836bb3362339bd168d66d436cb7";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
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;
}
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){
token.setToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
}
return token;
}
}
/Weixin/src/com/imooc/test/WeixinTest.java
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());
}
}
// 第一次获取的
//票据:12_kFXPkJ3GTDemDskOwVe2EX-qoDztAUl4sTNRI5sxbEmS-TkpJ7Z80lTJueFnXb3xRDpqSNd6jeKTyBecdHGOveGtHnihS3zULr8i7DQXH9TwVrDAvoM-WYyd3njM8J_Tbf1-VWeOBYk6qpjhHOCiAFAXE
//有效时间:7200