Package Com.wanhua.weixin.model;
Import Com.alibaba.fastjson.JSON;
Import Com.alibaba.fastjson.JSONObject;
Import Com.wanhua.weixin.util.WXConst;
Import Com.wanhua.weixin.util.WXHttpUtil;
/**
* Website Authorization token
*
* @author Enway
*
*/
public class Oauthtoken {
The web authorization interface invokes the credential, note: This access_token differs from the underlying supported Access_token
Public String Access_token;
Voucher valid time, unit: seconds
public long expires_in;
User Refresh Access_token
Public String Refresh_token;
User Unique identification
Public String OpenID;
User-authorized scopes, separated by commas (,)
public String scope;
public int errcode;
Public String errmsg;
/**
* Get credentials for the Web authorization interface
*
* @param code
* @return
* @throws Exception
*/
public static Oauthtoken Getoauthtoken (String code) throws Exception {
The requested URL
String urlstr = String.Format (Wxconst.url_get_oauth_token, code);
The result of the request
String result = Wxhttputil.msghttpsrequest (Urlstr, "POST", null);
Website Authorization token
Oauthtoken Oauthtoken = Json.parseobject (result, oauthtoken.class);
if (Oauthtoken.errcode! = 0) {
throw new Exception ("Get voucher Failed");
}
return oauthtoken;
}
/**
* Verify that the authorization voucher (Access_token) is valid
*
* @param access_token
* @param OpenID
* @return
*/
public static Boolean Verifyoauthtoken (String Access_token, String OpenID) {
The requested URL
String urlstr = String.Format (Wxconst.url_verify_oauth_token, Access_token, OpenID);
try {
The result of the request
String result = Wxhttputil.msghttpsrequest (Urlstr, "POST", null);
System.out.println ("Verify Oauth token==>" + result);
Convert request results to JSON-formatted data
Jsonobject Jsonobject = json.parseobject (result);
Status code for request result
int code = Jsonobject.getinteger ("Errcode");
Request succeeded
if (code = = 0) {
return true;
} else {
Request failed
return false;
}
} catch (Exception e) {
E.printstacktrace ();
return false;
}
}
}
Website Authorization token