| 1 | package QQ::weixin::work; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =encoding utf8 | ||
| 6 | |||
| 7 | =head1 Name | ||
| 8 | |||
| 9 | QQ::weixin::work | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | 腾讯企业微信->服务端API接口文档 | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | L<https://work.weixin.qq.com/api/doc/90000/90135/90664> | ||
| 18 | |||
| 19 | 服务端API开放了丰富的能力接口,开发者可以借助接口能力,实现企业服务及企业微信的集成。 | ||
| 20 | |||
| 21 | 支持的能力,通过目录导航可以快速预览,目录树按功能块聚合归类,如通讯录管理、消息推送等。 | ||
| 22 | |||
| 23 | =head2 企业开发流程如下: | ||
| 24 | |||
| 25 | 1.获取企业微信的CorpID和CorpSecret:企业微信管理员通过启用应用,获取CorpID和CorpSecret | ||
| 26 | |||
| 27 | 2.开发对接相关接口:开发测试应用,对接企业微信接口 | ||
| 28 | |||
| 29 | =head1 权限说明 | ||
| 30 | |||
| 31 | 每个应用有不同的secret,代表了对应用的不同权限 | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use Encode; | ||
| 37 | use LWP::UserAgent; | ||
| 38 | use JSON; | ||
| 39 | use utf8; | ||
| 40 | |||
| 41 | our $VERSION = '0.12'; | ||
| 42 | our @EXPORT = qw/ gettoken getcallbackip get_api_domain_ip /; | ||
| 43 | |||
| 44 | =head1 FUNCTION | ||
| 45 | |||
| 46 | =head2 gettoken(corpid,corpsecrect); | ||
| 47 | |||
| 48 | 获取ACCESS_TOKEN | ||
| 49 | |||
| 50 | =head3 SYNOPSIS | ||
| 51 | |||
| 52 | L<https://developer.work.weixin.qq.com/document/path/91039> | ||
| 53 | |||
| 54 | =head3 参数说明 | ||
| 55 | |||
| 56 | 参数 必须 说明 | ||
| 57 | corpid 是 企业ID,获取方式参考:术语说明-corpid L<https://work.weixin.qq.com/api/doc/90000/90135/91039#14953/corpid> | ||
| 58 | corpsecret 是 应用的凭证密钥,获取方式参考:术语说明-secret L<https://work.weixin.qq.com/api/doc/90000/90135/91039#14953/secret> | ||
| 59 | |||
| 60 | =head3 RETURN 返回结果 | ||
| 61 | |||
| 62 | { | ||
| 63 | "errcode": 0, | ||
| 64 | "errmsg": "ok", | ||
| 65 | "access_token": "accesstoken000001", | ||
| 66 | "expires_in": 7200 | ||
| 67 | } | ||
| 68 | |||
| 69 | =head4 RETURN 参数说明 | ||
| 70 | |||
| 71 | 参数 说明 | ||
| 72 | errcode 出错返回码,为0表示成功,非0表示调用失败 | ||
| 73 | errmsg 返回码提示语 | ||
| 74 | access_token 获取到的凭证,最长为512字节 | ||
| 75 | expires_in 凭证的有效时间(秒) | ||
| 76 | |||
| 77 | =head3 注意事项 | ||
| 78 | |||
| 79 | 开发者需要缓存access_token,用于后续接口的调用(注意:不能频繁调用gettoken接口,否则会受到频率拦截)。当access_token失效或过期时,需要重新获取。 | ||
| 80 | |||
| 81 | access_token的有效期通过返回的expires_in来传达,正常情况下为7200秒(2小时),有效期内重复获取返回相同结果,过期后获取会返回新的access_token。 | ||
| 82 | |||
| 83 | 由于企业微信每个应用的access_token是彼此独立的,所以进行缓存时需要区分应用来进行存储。 | ||
| 84 | |||
| 85 | access_token至少保留512字节的存储空间。 | ||
| 86 | |||
| 87 | 企业微信可能会出于运营需要,提前使access_token失效,开发者应实现access_token失效时重新获取的逻辑。 | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub gettoken { | ||
| 92 | if ( @_ && $_[0] && $_[1] ) { | ||
| 93 | my $corpid = $_[0]; | ||
| 94 | my $corpsecret = $_[1]; | ||
| 95 | my $ua = LWP::UserAgent->new; | ||
| 96 | $ua->timeout(30); | ||
| 97 | $ua->env_proxy; | ||
| 98 | |||
| 99 | my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret"); | ||
| 100 | if ($response->is_success) { | ||
| 101 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 102 | } | ||
| 103 | |||
| 104 | } | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 getcallbackip(access_token); | ||
| 109 | |||
| 110 | 获取企业微信服务器的ip段 | ||
| 111 | 最后更新:2023/11/29 | ||
| 112 | |||
| 113 | =head3 SYNOPSIS | ||
| 114 | |||
| 115 | L<https://developer.work.weixin.qq.com/document/path/90238#获取企业微信服务器的ip段> | ||
| 116 | |||
| 117 | =head3 请求说明: | ||
| 118 | |||
| 119 | 企业微信在回调企业指定的URL时,是通过特定的IP发送出去的。如果企业需要做防火墙配置,那么可以通过这个接口获取到所有相关的IP段。IP段有变更可能,当IP段变更时,新旧IP段会同时保留一段时间。建议企业每天定时拉取IP段,更新防火墙设置,避免因IP段变更导致网络不通。 | ||
| 120 | |||
| 121 | =head3 参数说明 | ||
| 122 | |||
| 123 | 参数 必须 说明 | ||
| 124 | access_token 是 调用接口凭证 | ||
| 125 | |||
| 126 | =head3 RETURN 返回结果 | ||
| 127 | |||
| 128 | { | ||
| 129 | "errcode": 0, | ||
| 130 | "errmsg": "ok", | ||
| 131 | "ip_list": ["101.226.103.*", "101.226.62.*"] | ||
| 132 | } | ||
| 133 | |||
| 134 | =head4 RETURN 参数说明 | ||
| 135 | |||
| 136 | 参数 类型 说明 | ||
| 137 | errcode int 错误码,0表示成功,非0表示调用失败 | ||
| 138 | errmsg string 错误信息,调用失败会有相关的错误信息返回 | ||
| 139 | ip_list StringArray 企业微信回调的IP段 | ||
| 140 | |||
| 141 | =head3 注意事项 | ||
| 142 | |||
| 143 | 若调用失败,会返回errcode及errmsg(判断是否调用失败,根据errcode存在并且值非0) | ||
| 144 | |||
| 145 | 根据errcode值非0,判断调用失败。以下是access_token过期的返回示例: | ||
| 146 | |||
| 147 | { | ||
| 148 | "ip_list":[], | ||
| 149 | "errcode":42001, | ||
| 150 | "errmsg":"access_token expired, hint: [1576065934_28_e0fae07666aa64636023c1fa7e8f49a4], from ip: 9.30.0.138, more info at https://open.work.weixin.qq.com/devtool/query?e=42001" | ||
| 151 | } | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub getcallbackip { | ||
| 156 | if ( @_ && $_[0] ) { | ||
| 157 | my $access_token = $_[0]; | ||
| 158 | my $ua = LWP::UserAgent->new; | ||
| 159 | $ua->timeout(30); | ||
| 160 | $ua->env_proxy; | ||
| 161 | |||
| 162 | my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/getcallbackip?access_token=$access_token"); | ||
| 163 | if ($response->is_success) { | ||
| 164 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 165 | } | ||
| 166 | |||
| 167 | } | ||
| 168 | return 0; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 get_api_domain_ip(access_token); | ||
| 172 | |||
| 173 | 获取企业微信API域名IP段 | ||
| 174 | |||
| 175 | =head3 SYNOPSIS | ||
| 176 | |||
| 177 | L<https://developer.work.weixin.qq.com/document/path/92520> | ||
| 178 | API域名IP即qyapi.weixin.qq.com的解析地址,由开发者调用企业微信端的接入IP。如果企业需要做防火墙配置,那么可以通过这个接口获取到所有相关的IP段。IP段有变更可能,当IP段变更时,新旧IP段会同时保留一段时间。建议企业每天定时拉取IP段,更新防火墙设置,避免因IP段变更导致网络不通。 | ||
| 179 | |||
| 180 | =head3 参数说明 | ||
| 181 | |||
| 182 | 参数 必须 说明 | ||
| 183 | access_token 是 调用接口凭证 | ||
| 184 | |||
| 185 | =head3 RETURN 返回结果 | ||
| 186 | |||
| 187 | { | ||
| 188 | "ip_list":[ | ||
| 189 | "182.254.11.176", | ||
| 190 | "182.254.78.66" | ||
| 191 | ], | ||
| 192 | "errcode":0, | ||
| 193 | "errmsg":"ok" | ||
| 194 | } | ||
| 195 | |||
| 196 | =head4 RETURN 参数说明 | ||
| 197 | |||
| 198 | 参数 类型 说明 | ||
| 199 | ip_list StringArray 企业微信回调的IP段 | ||
| 200 | errcode int 错误码,0表示成功,非0表示调用失败 | ||
| 201 | errmsg string 错误信息,调用失败会有相关的错误信息返回 | ||
| 202 | |||
| 203 | =head3 注意事项 | ||
| 204 | |||
| 205 | 若调用失败,会返回errcode及errmsg(判断是否调用失败,根据errcode存在并且值非0) | ||
| 206 | |||
| 207 | 根据errcode值非0,判断调用失败。以下是access_token过期的返回示例: | ||
| 208 | |||
| 209 | { | ||
| 210 | "ip_list":[], | ||
| 211 | "errcode":42001, | ||
| 212 | "errmsg":"access_token expired, hint: [1576065934_28_e0fae07666aa64636023c1fa7e8f49a4], from ip: 9.30.0.138, more info at https://open.work.weixin.qq.com/devtool/query?e=42001" | ||
| 213 | } | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub get_api_domain_ip { | ||
| 218 | if ( @_ && $_[0] ) { | ||
| 219 | my $access_token = $_[0]; | ||
| 220 | my $ua = LWP::UserAgent->new; | ||
| 221 | $ua->timeout(30); | ||
| 222 | $ua->env_proxy; | ||
| 223 | |||
| 224 | my $response = $ua->get("https://qyapi.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token=$access_token"); | ||
| 225 | if ($response->is_success) { | ||
| 226 | return from_json($response->decoded_content,{utf8 => 1, allow_nonref => 1}); | ||
| 227 | } | ||
| 228 | |||
| 229 | } | ||
| 230 | return 0; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; | ||
| 234 | __END__ |