[java]代码库
package cn.elve.jxt.weixin.weixin.util;
import java.net.URLEncoder;
import cn.elve.jxt.weixin.util.Constants;
/**
* 获取微信的code
* @author 宗潇帅
* @修改日期 2014-7-21下午1:01:45
*/
public class GetWeiXinCode {
public static String GetCodeRequest = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
public static String getCodeRequest(){
String result = null;
GetCodeRequest = GetCodeRequest.replace("APPID", urlEnodeUTF8(Constants.appId));
GetCodeRequest = GetCodeRequest.replace("REDIRECT_URI",urlEnodeUTF8(Constants.REDIRECT_URI));
GetCodeRequest = GetCodeRequest.replace("SCOPE", Constants.SCOPE);
result = GetCodeRequest;
return result;
}
public static String urlEnodeUTF8(String str){
String result = str;
try {
result = URLEncoder.encode(str,"UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println(getCodeRequest());
}
}//源代码片段来自云代码http://yuncode.net
获取code后,请求以下链接获取access_token:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code//源代码片段来自云代码http://yuncode.net