微信公衆帳號登錄受權開發——4

獲取預受權碼

獲取到access_token後就能夠獲取pre_auth_code預受權碼了

-------------------------------------------------------------------------官方文檔說明開始-----------------------------------------------------------------------------------

java


二、獲取預受權碼

該API用於獲取預受權碼。預受權碼用於公衆號受權時的第三方平臺方安全驗證。json

接口調用請求說明

http請求方式: POST(請使用https協議)api

https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx安全

POST數據示例:app

{
"component_appid":"appid_value" 
}
   this

請求參數說明
參數 說明
component_appid 第三方平臺方appid
返回結果示例

{
"pre_auth_code":"Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw",
"expires_in":600
}
   url

結果參數說明
參數 說明
pre_auth_code 預受權碼
expires_in 有效期,爲20分鐘

----------------------------------------------------官方文檔說明結束--------------------------------------------------------------------------------

/**
     * 二、獲取預受權碼
     * 
     * http請求方式: POST(請使用https協議)
     * 
     * https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?
     * component_access_token=xxx
     * 
     * POST數據示例: { "component_appid":"appid_value" }
     */
    public static String getPre_auth_code() {
        String url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + getToken();
        Pre_auth_code pre = new Pre_auth_code();
        pre.setComponent_appid("*****");
        JSONObject jsonObject = WeixinUtil.httpRequest(url, "POST", JSONObject.fromObject(pre).toString());
        System.out.println("獲取的預受權碼爲:  " + jsonObject.toString());
        return jsonObject.getString("pre_auth_code");
    }

將獲取預受權碼的參數封裝成爲一個bean

/**
 * 二、獲取預受權碼
 * 
 * 該API用於獲取預受權碼。預受權碼用於公衆號受權時的第三方平臺方安全驗證。
 * 
 * @author YangChao
 * 
 */
public class Pre_auth_code {

    // 第三方平臺方appid
    private String component_appid;

    public String getComponent_appid() {
        return component_appid;
    }

    public void setComponent_appid(String component_appid) {
        this.component_appid = component_appid;
    }

}




版權聲明:本文爲博主原創文章,未經博主容許不得轉載。spa

相關文章
相關標籤/搜索