企业微信 获取服务商凭证

原文:http://www.upwqy.com/doc/32.html

获取服务商凭证

请求方式:POST(HTTPS)
请求地址: https://qyapi.weixin.qq.com/cgi-bin/service/get_provider_token

请求包体:

  1. {
  2. "corpid":"xxxxx",
  3. "provider_secret":"xxx"
  4. }

参数说明:

参数是否必须说明
corpid 服务商的corpid
provider_secret 服务商的secret,在服务商管理后台可见

返回结果:

  1. {
  2. "provider_access_token":"enLSZ5xxxxxxJRL",
  3. "expires_in":7200
  4. }

参数说明:

参数说明
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;
    }
原文地址:https://www.cnblogs.com/wqy415/p/13084458.html