原文:http://www.upwqy.com/doc/32.html
獲取服務商憑證
請求方式:POST(HTTPS)
請求地址: https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token
請求包體:
{
"corpid":"xxxxx",
"provider_secret":"xxx"
}
參數說明:
參數 | 是否必須 | 說明 |
---|---|---|
corpid | 是 | 服務商的corpid |
provider_secret | 是 | 服務商的secret,在服務商管理后台可見 |
返回結果:
{
"provider_access_token":"enLSZ5xxxxxxJRL",
"expires_in":7200
}
參數說明:
參數 | 說明 |
---|---|
provider_access_token | 服務商的access_token,最長為512字節。 |
expires_in | provider_access_token有效期(秒) |
若調用失敗,會返回errcode及errmsg字段。(開發者根據errcode字段存在且值非0,可認為是調用失敗)
注意事項:
開發者需要緩存provider_access_token,用於后續接口的調用(注意:不能頻繁調用get_provider_token接口,否則會受到頻率攔截)。當provider_access_token失效或過期時,需要重新獲取。
provider_access_token的有效期通過返回的expires_in來傳達,正常情況下為7200秒(2小時),有效期內重復獲取返回相同結果,過期后獲取會返回新的provider_access_token。
provider_access_token至少保留512字節的存儲空間。
企業微信可能會出於運營需要,提前使provider_access_token失效,開發者應實現provider_access_token失效時重新獲取的邏輯。
/** * @param string $auth_corpid 授權方企業id * @param string $permanent_code 企業永久授權碼 * @param $suite_access_token 第三方應用憑證 * @return mixed * @throws ParameterException */ public function getAccessToken($auth_corpid,$permanent_code,$suite_access_token) { $path = cache_path.$auth_corpid."_getAccessToken.php"; $data = json_decode(get_php_file($path)); if($data->expire_time < time()) { $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token?suite_access_token=".$suite_access_token; $params = [ 'auth_corpid'=>$auth_corpid, 'permanent_code'=>$permanent_code ]; $res = json_decode(http_post($url,$params)["content"]); if(isset($res->errcode) && $res->errcode){ throw new ParameterException($res->errmsg); } $access_token = $res->access_token; if($access_token) { $data->expire_time = time() + $res->expires_in - 200; $data->access_token = $access_token; set_php_file($path, json_encode($data)); } } else { $access_token = $data->access_token; } return $access_token; }