package com.qunzhixinxi.hnqz.admin.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取accessToken
 *
 */
public class GetAccessTokenUtil {
	// 网页授权接口
	public static Map<String, String> getAccessToken(String appid, String appsecret) {
		String requestUrl ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret;
		HttpClient client = null;
		Map<String, String> result = new HashMap<String, String>();
		String accessToken = null;
		try {
			client = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(requestUrl);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = client.execute(httpget, responseHandler);
			JSONObject OpenidJSONO = JSONObject.parseObject(response);
			accessToken = String.valueOf(OpenidJSONO.get("access_token"));
			result.put("accessToken", accessToken);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
		return result;
	}
}
