Website Authorization token

Source: Internet
Author: User
Tags openid

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.