chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:31:34 +08:00
commit 665ce57fb8
391 changed files with 780423 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
"""
@File : __init__.py.py
@Date : 2023-06-22
第三放开放平台接口
"""
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
"""
@File : aliyun_cas_api.py
@Date : 2024-07-24
"""
from alibabacloud_cas20200407 import models as cas_20200407_models
from alibabacloud_cas20200407.client import Client as cas20200407Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from domain_admin.utils import uuid_util
def upload_user_certificate(
access_key_id, access_key_secret,
cert_name, cert, key
):
"""
上传证书
@return: cert_id
@throws Exception
https://api.aliyun.com/api-tools/sdk/cas?spm=api-workbench.api_explorer.0.0.74935d8cS3kyPm&version=2020-04-07&language=python-tea&tab=primer-doc
"""
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
# 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
config = open_api_models.Config(
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
access_key_id=access_key_id,
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
access_key_secret=access_key_secret
)
# Endpoint 请参考 https://api.aliyun.com/product/cas
config.endpoint = 'cas.aliyuncs.com'
client = cas20200407Client(config)
upload_user_certificate_request = cas_20200407_models.UploadUserCertificateRequest(
name=cert_name,
cert=cert,
key=key,
)
runtime = util_models.RuntimeOptions()
response = client.upload_user_certificate_with_options(upload_user_certificate_request, runtime)
return response.body.cert_id
@@ -0,0 +1,121 @@
# -*- coding: utf-8 -*-
"""
@File : aliyun_cdn_api.py
@Date : 2024-07-24
"""
from alibabacloud_cdn20180510 import models as cdn_20180510_models
from alibabacloud_cdn20180510.client import Client as Cdn20180510Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
from domain_admin.utils import uuid_util
from domain_admin.utils.open_api import aliyun_cas_api
def set_cdn_domain_ssl_certificate(
access_key_id, access_key_secret,
domain_name,
cert_id, cert_name):
"""
https://api.aliyun.com/api-tools/sdk/Cdn?spm=api-workbench.api_explorer.0.0.539f3761ceDHDv&version=2018-05-10&language=python-tea&tab=primer-doc
:param access_key_id:
:param access_key_secret:
:param domain_name:
:param cert_id:
:return:
"""
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
# 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
config = open_api_models.Config(
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
access_key_id=access_key_id,
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
access_key_secret=access_key_secret,
)
# Endpoint 请参考 https://api.aliyun.com/product/Cdn
config.endpoint = 'cdn.aliyuncs.com'
client = Cdn20180510Client(config)
set_cdn_domain_sslcertificate_request = cdn_20180510_models.SetCdnDomainSSLCertificateRequest(
cert_id=cert_id,
cert_name=cert_name,
cert_type='cas',
domain_name=domain_name,
sslprotocol='on'
)
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
client.set_cdn_domain_sslcertificate_with_options(set_cdn_domain_sslcertificate_request, runtime)
def set_cdn_domain_cert(
access_key_id, access_key_secret,
domain,
certificate, private_key):
"""
先上传,再部署
:param access_key_id:
:param access_key_secret:
:param domain:
:param certificate:
:param private_key:
:return:
"""
cert_name = uuid_util.get_uuid()
cert_id = aliyun_cas_api.upload_user_certificate(
access_key_id=access_key_id,
access_key_secret=access_key_secret,
cert_name=cert_name,
cert=certificate,
key=private_key
)
print('cert_id: ', cert_id)
set_cdn_domain_ssl_certificate(
access_key_id=access_key_id,
access_key_secret=access_key_secret,
domain_name=domain,
cert_id=cert_id,
cert_name=cert_name
)
def set_cdn_domain_ssl_certificate_v2(
access_key_id, access_key_secret,
domain_name,
certificate, private_key
):
"""
直接部署到CDN
https://api.aliyun.com/api-tools/sdk/Cdn?spm=api-workbench.api_explorer.0.0.539f3761ceDHDv&version=2018-05-10&language=python-tea&tab=primer-doc
:return:
"""
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
# 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
config = open_api_models.Config(
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
access_key_id=access_key_id,
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
access_key_secret=access_key_secret,
)
# Endpoint 请参考 https://api.aliyun.com/product/Cdn
config.endpoint = 'cdn.aliyuncs.com'
client = Cdn20180510Client(config)
set_cdn_domain_sslcertificate_request = cdn_20180510_models.SetCdnDomainSSLCertificateRequest(
domain_name=domain_name,
cert_type='upload',
sslpri=private_key,
sslpub=certificate,
sslprotocol='on'
)
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
client.set_cdn_domain_sslcertificate_with_options(set_cdn_domain_sslcertificate_request, runtime)
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
aliyun_dcdn_api.py
"""
# This file is auto-generated, don't edit it. Thanks.
from alibabacloud_dcdn20180115 import models as dcdn_20180115_models
from alibabacloud_dcdn20180115.client import Client as dcdn20180115Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_tea_util import models as util_models
def set_dcdn_domain_ssl_certificate(
access_key_id, access_key_secret,
domain_name,
certificate, private_key):
"""
https://api.aliyun.com/api/dcdn/2018-01-15/SetDcdnDomainSSLCertificate?spm=api-workbench.API%20Document.0.0.26c93c7bP4PZn9&tab=DOC&lang=PYTHON&params={%22DomainName%22:%22www%22,%22SSLProtocol%22:%22on%22,%22SSLPub%22:%22xxx%22,%22SSLPri%22:%22xxx%22}
"""
# 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
# 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378659.html。
config = open_api_models.Config(
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
access_key_id=access_key_id,
# 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
access_key_secret=access_key_secret
)
# Endpoint 请参考 https://api.aliyun.com/product/dcdn
config.endpoint = 'dcdn.aliyuncs.com'
client = dcdn20180115Client(config)
set_dcdn_domain_sslcertificate_request = dcdn_20180115_models.SetDcdnDomainSSLCertificateRequest(
domain_name=domain_name,
sslprotocol='on',
sslpub=certificate,
sslpri=private_key
)
runtime = util_models.RuntimeOptions()
# 复制代码运行请自行打印 API 的返回值
client.set_dcdn_domain_sslcertificate_with_options(set_dcdn_domain_sslcertificate_request, runtime)
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-
"""
@File : aliyun_domain_api.py
@Date : 2024-06-17
"""
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.client import AcsClient
from domain_admin.log import logger
class RecordTypeEnum:
"""
记录类型枚举
ref: https://help.aliyun.com/zh/dns/dns-record-types
"""
A = 'A'
TXT = 'TXT'
def add_domain_record(
access_key_id, access_key_secret,
domain_name, record_key, record_type, record_value
):
"""
添加域名解析记录
doc:
https://next.api.aliyun.com/api-tools/sdk/Alidns?version=2015-01-09&language=python&tab=primer-doc
https://next.api.aliyun.com/api/Alidns/2015-01-09/AddDomainRecord?sdkStyle=old&tab=DEMO&lang=PYTHON
:param access_key_id: key
:param access_key_secret: secret
:param domain_name: 域名名称
:param record_key: 主机记录
:param record_type: 解析记录类型 RecordTypeEnum
:param record_value: 记录值
:return:
"""
logger.info("%s", {
'access_key_id': access_key_id,
'access_key_secret': access_key_secret,
'domain_name': domain_name,
'record_key': record_key,
'record_type': record_type,
'record_value': record_value,
})
# Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
credentials = AccessKeyCredential(
access_key_id=access_key_id,
access_key_secret=access_key_secret
)
# use STS Token
# credentials = StsTokenCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'], os.environ['ALIBABA_CLOUD_SECURITY_TOKEN'])
client = AcsClient(region_id='cn-beijing', credential=credentials)
request = AddDomainRecordRequest()
request.set_accept_format('json')
request.set_DomainName(domain_name)
request.set_RR(record_key)
request.set_Type(record_type)
request.set_Value(record_value)
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
@@ -0,0 +1,130 @@
# -*- coding: utf-8 -*-
"""
@File : aliyun_oss_api.py
@Date : 2024-02-02
@Author : Peng Shiyu
OSS Python SDK适用于Python 2.6、2.7、3.3、3.4、3.5、3.6、3.7、3.8及以上版本。
https://help.aliyun.com/zh/oss/developer-reference/map-custom-domain-names-4?spm=a2c4g.11186623.0.i6
"""
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider, StaticCredentialsProvider
# https://next.api.aliyun.com/product/Oss
from domain_admin.utils import domain_util, dns_util
ENDPOINT_OPTIONS = [
{
'label': '华北1(青岛)',
'value': 'cn-qingdao',
'endpoint': 'https://oss-cn-qingdao.aliyuncs.com',
},
{
'label': '华北2(北京)',
'value': 'cn-beijing',
'endpoint': 'https://oss-cn-beijing.aliyuncs.com',
},
{
'label': '华北3(张家口)',
'value': 'cn-zhangjiakou',
'endpoint': 'https://oss-cn-zhangjiakou.aliyuncs.com',
},
{
'label': '华北6(乌兰察布)',
'value': 'cn-wulanchabu',
'endpoint': 'https://oss-cn-wulanchabu.aliyuncs.com',
},
{
'label': '华东1(杭州)',
'value': 'cn-hangzhou',
'endpoint': 'https://oss-cn-hangzhou.aliyuncs.com',
},
{
'label': '华东2(上海)',
'value': 'cn-shanghai',
'endpoint': 'https://oss-cn-shanghai.aliyuncs.com',
},
{
'label': '华南1(深圳)',
'value': 'cn-shenzhen',
'endpoint': 'https://oss-cn-shenzhen.aliyuncs.com',
},
{
'label': '华南3(广州)',
'value': 'cn-guangzhou',
'endpoint': 'https://oss-cn-guangzhou.aliyuncs.com',
},
{
'label': '西南1(成都)',
'value': 'cn-chengdu',
'endpoint': 'https://oss-cn-chengdu.aliyuncs.com',
}
]
def get_endpoint_by_value(value):
for item in ENDPOINT_OPTIONS:
if item['value'] == value:
return item['endpoint']
def cname_to_oss_info(cname):
"""
'zaiting.oss-cn-beijing.aliyuncs.com.'
:param cname:
:return: zaiting oss-cn-beijing.aliyuncs.com
"""
lst = dns_util.query_domain_cname(cname)
if lst and len(lst) > 0:
domain = lst[0]
return {
'bucket_name': domain.split('.')[0],
'endpoint': 'https://' + domain_util.get_domain_parent(domain).strip('.')
}
else:
return None
def put_bucket_cname(
access_key_id,
access_key_secret,
bucket_name,
domain,
certificate,
private_key,
endpoint='https://oss-cn-beijing.aliyuncs.com',
):
"""
将证书部署到oss
:param access_key_id:
:param access_key_secret:
:param bucket_name:
:param domain:
:param certificate:
:param private_key:
:param endpoint:
:return:
"""
# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.ProviderAuth(
StaticCredentialsProvider(access_key_id=access_key_id, access_key_secret=access_key_secret)
)
# yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# 填写Bucket名称,例如examplebucket。
bucket = oss2.Bucket(
auth=auth,
# https://oss-cn-beijing.aliyuncs.com
endpoint=endpoint,
bucket_name=bucket_name
)
cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key)
# 通过force=True设置强制覆盖旧版证书。
# 通过delete_certificate选择是否删除证书。设置为delete_certificate=True表示删除证书,设置为delete_certificate=False表示不删除证书。
# cert = oss2.models.CertInfo(certificate=certificate, private_key=private_key, force=True, delete_certificate=False)
input = oss2.models.PutBucketCnameRequest(domain, cert)
bucket.put_bucket_cname(input)
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
from domain_admin.log import logger
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
def search(domain, include_subdomains=True):
"""
查询 Cert Spotter 证书签发记录
:param domain: str
:param include_subdomains: bool
:return: list
"""
url = 'https://api.certspotter.com/v1/issuances'
params = {
'domain': domain,
'include_subdomains': 'true' if include_subdomains else 'false',
'expand': 'dns_names',
}
headers = {
'User-Agent': USER_AGENT
}
try:
req = requests.get(url=url, params=params, headers=headers, timeout=8)
except Exception:
logger.error('certspotter request failed: %s', domain)
return []
if not req.ok:
logger.warn('certspotter request not ok: status_code=%s', req.status_code)
return []
try:
return req.json()
except ValueError:
logger.warn('certspotter response is not json, status_code=%s', req.status_code)
return []
+50
View File
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
"""
@File : crtsh_api.py
@Date : 2023-07-10
参考:
https://crt.sh/
https://github.com/PaulSec/crt.sh
需求:https://github.com/mouday/domain-admin/issues/41
"""
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
from domain_admin.log import logger
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
def search(domain):
"""
搜索子域证书列表
:param domain: str 顶级域名
:return:
"""
url = "https://crt.sh/"
params = {
'q': domain,
'output': 'json'
}
headers = {
'User-Agent': USER_AGENT
}
req = requests.get(url=url, params=params, headers=headers, timeout=8)
try:
return req.json()
except ValueError:
logger.warn('crt.sh response is not json, status_code=%s', req.status_code)
return []
if __name__ == '__main__':
# lst = search('bilibili.com')
lst = search('baidu.com')
print([row['common_name'] for row in lst])
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
"""
@File : ding_talk_api.py
@Date : 2023-06-22
钉钉开放API接口
"""
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
def get_access_token(appkey, appsecret):
"""
获取access_token
https://open.dingtalk.com/document/orgapp/obtain-orgapp-token
:param appkey: 应用的唯一标识key
:param appsecret: 应用的密钥
:return:
{
"errcode": 0,
"access_token": "96fc7a7axxx",
"errmsg": "ok",
"expires_in": 7200
}
"""
url = 'https://oapi.dingtalk.com/gettoken'
params = {
'appkey': appkey,
'appsecret': appsecret
}
res = requests.get(url, params=params)
return res.json()
def send_message(access_token, body):
"""
发送应用消息
https://open.dingtalk.com/document/orgapp/asynchronous-sending-of-enterprise-session-messages
:param access_token:
:param body: 消息体
:return:
{
"errcode":0,
"task_id":256271667526,
"request_id":"4jzllmte0wau"
}
"""
url = 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2'
params = {
'access_token': access_token,
}
res = requests.post(url, params=params, json=body)
return res.json()
+57
View File
@@ -0,0 +1,57 @@
# -*- coding: utf-8 -*-
"""
@File : feishu_api.py
@Date : 2023-06-22
飞书开放API接口
"""
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
def get_access_token(app_id, app_secret):
"""
自建应用获取 tenant_access_token
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
:param app_id: 应用唯一标识
:param app_secret: 应用秘钥
:return:
{
"code": 0,
"msg": "success",
"tenant_access_token": "t-caecc734c2e3328a62489fe0648c4b98779515d3",
"expire": 7140
}
"""
url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
params = {
'app_id': app_id,
'app_secret': app_secret
}
res = requests.post(url, params=params)
return res.json()
def send_message(access_token, body, params):
"""
发送消息
https://open.feishu.cn/document/server-docs/im-v1/message/create
:param access_token:
:param body: 消息体
:param params: 查询参数 {"receive_id_type":"open_id"}
:return:
"""
url = 'https://open.feishu.cn/open-apis/im/v1/messages'
headers = {
'Authorization': 'Bearer ' + access_token
}
res = requests.post(url, params=params, headers=headers, json=body)
return res.json()
+90
View File
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
from domain_admin.log import logger
def search_domain_certificates(domain, api_key):
"""
使用 Shodan DNS domain API 查询域名证书索引
:param domain: str
:param api_key: str
:return: list
"""
if not api_key:
return []
url = "https://api.shodan.io/dns/domain/{domain}".format(domain=domain)
params = {
'key': api_key
}
try:
req = requests.get(url=url, params=params, timeout=8)
except Exception:
logger.error('shodan request failed: %s', domain)
return []
if not req.ok:
if req.status_code == 404:
return []
logger.warn('shodan request not ok: status_code=%s', req.status_code)
return []
try:
data = req.json()
except ValueError:
logger.warn('shodan response is not json, status_code=%s', req.status_code)
return []
return data.get('data', []) if isinstance(data, dict) else []
def get_certificate_by_sha1(sha1, api_key):
"""
通过证书指纹查询证书详情
:param sha1: str
:param api_key: str
:return: dict
"""
if not api_key or not sha1:
return {}
url = "https://api.shodan.io/shodan/host/search"
params = {
'key': api_key,
'query': 'ssl.cert.fingerprint:{sha1}'.format(sha1=sha1),
'facets': '',
'minify': 'false'
}
try:
req = requests.get(url=url, params=params, timeout=8)
except Exception:
logger.error('shodan cert detail request failed: %s', sha1)
return {}
if not req.ok:
logger.warn('shodan cert detail request not ok: status_code=%s', req.status_code)
return {}
try:
data = req.json()
except ValueError:
logger.warn('shodan cert detail response is not json, status_code=%s', req.status_code)
return {}
matches = data.get('matches') if isinstance(data, dict) else None
if not matches:
return {}
match = matches[0] if isinstance(matches, list) else {}
ssl_data = match.get('ssl') if isinstance(match, dict) else {}
cert = ssl_data.get('cert') if isinstance(ssl_data, dict) else {}
return cert if isinstance(cert, dict) else {}
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
@File : telegram_api.py
@Date : 2024-05-28
"""
import requests
def send_message(token, chat_id, text, proxies):
"""
发送应用消息
ref https://mp.weixin.qq.com/s/dvQiP87LQYP7ssx4zELb1w
:return:
"""
url = 'https://api.telegram.org/bot{token}/sendMessage'.format(
token=token
)
data = {
'chat_id': chat_id,
'text': text,
}
res = requests.post(url=url, data=data, proxies=proxies)
return res.json()
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-
"""
@File : tencentcloud_domain_api.py
@Date : 2024-06-28
"""
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models
def add_domain_record(
access_key_id, access_key_secret,
domain_name, record_key, record_type, record_value
):
"""
https://cloud.tencent.com/document/api/1427/56180
:param access_key_id:
:param access_key_secret:
:param domain_name:
:param record_key:
:param record_type:
:param record_value:
:return:
"""
# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
cred = credential.Credential(
secret_id=access_key_id,
secret_key=access_key_secret
)
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = dnspod_client.DnspodClient(cred, "", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.CreateRecordRequest()
params = {
"Domain": domain_name,
"RecordType": record_type,
"RecordLine": "默认",
"Value": record_value,
"SubDomain": record_key
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个CreateRecordResponse的实例,与请求对象对应
resp = client.CreateRecord(req)
# 输出json格式的字符串回包
return resp.to_json_string()
@@ -0,0 +1,64 @@
# -*- coding: utf-8 -*-
"""
@File : api.py
@Date : 2023-03-08
企业微信开放API接口
"""
from __future__ import print_function, unicode_literals, absolute_import, division
import requests
def get_access_token(corpid, corpsecret):
"""
获取access_token
https://developer.work.weixin.qq.com/document/path/91039
:param corpid: 企业ID
:param corpsecret: 应用的凭证密钥
:return:
{
"errcode": 0,
"errmsg": "ok",
"access_token": "accesstoken000001",
"expires_in": 7200
}
"""
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
params = {
'corpid': corpid,
'corpsecret': corpsecret
}
res = requests.get(url, params=params)
return res.json()
def send_message(access_token, body):
"""
发送应用消息
https://developer.work.weixin.qq.com/document/path/90236
:param access_token:
:param body: 消息体
:return:
{
"errcode" : 0,
"errmsg" : "ok",
"invaliduser" : "userid1|userid2",
"invalidparty" : "partyid1|partyid2",
"invalidtag": "tagid1|tagid2",
"unlicenseduser" : "userid3|userid4",
"msgid": "xxxx",
"response_code": "xyzxyz"
}
"""
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send'
params = {
'access_token': access_token,
}
res = requests.post(url, params=params, json=body)
return res.json()