gzh_api.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import json
  2. import requests
  3. from applications.utils.async_http_client import AsyncHttpClient
  4. app_id = "wx96cd2914fe4d0ae9"
  5. app_secret = "04f8d6c3a4d61adbee64e9854197b2a9"
  6. class GzhApi:
  7. def __init__(self, app_id, app_secret):
  8. self.app_id = app_id
  9. self.app_secret = app_secret
  10. self.access_token = None
  11. async def get_access_token(self):
  12. url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={self.app_id}&secret={self.app_secret}"
  13. headers = {"Content-Type": "application/json;charset=UTF-8"}
  14. async with AsyncHttpClient() as client:
  15. response = await client.get(url, headers=headers)
  16. self.access_token = response.json()["access_token"]
  17. return self.access_token
  18. async def add_draft(self, data: Dict):
  19. url = f"https://api.weixin.qq.com/cgi-bin/draft/add?access_token={self.access_token}"
  20. headers = {"Content-Type": "application/json;charset=UTF-8"}
  21. async with AsyncHttpClient() as client:
  22. response = await client.post(url, json=data, headers=headers)
  23. return response.json()