在微信中检索Mediaid时遇到了503错误代码?

时间:2019-03-05 06:55:57

标签: python python-requests wechat

API http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files

import requests
r='https://api.wechat.com/cgi-bin/token?grant_type=client_credential&appid=wx82c6ebdb6e33ad33&secret=c2861ec348b3c94087c4b64cbe166fbb' #credentials sharing no problem
a=(requests.get(r).json()['access_token'])
print(a)
params = (
    ('access_token', a),
    ('type', 'image'),
)

import os
files = {
    'media': ('1.jpg', open('static/1.jpg', 'rb'),'image/jpg',),
}
print()
response = requests.post('http://file.api.wechat.com/cgi-bin/media/upload', params=params, files=files)

def uprint(x,file=None):
  try:
    pass
    if x:
      print(x.encode('cp65001',errors='backslashreplace').decode('cp1252'),file=file)
  except Exception as e:
    return f'{x}\n{e}'
def prin(*a):print(ascii(*a))    
print(response.text,file=open('z.html','a',encoding="utf-8"))
print(response.headers)

1 个答案:

答案 0 :(得分:1)

好像您正在使用http进行上载呼叫。以前,我已经看到从网站返回此错误。

HTTP错误503表示“服务不可用”。当服务器由于暂时的过载或维护而无法处理请求时,通常由服务器返回它。

在检查API documentation for wechat之后,我注意到了这一点:

  
      
  1. 此API必须通过HTTPS使用。
  2.   

然后我在the Q&A中注意到了这一点:

  

问::我应该向哪个服务器发送API请求?

     

A::如果您拥有国际正式帐户,请使用 api.wechat.com

     

如果您拥有中国官方帐户,请使用 api.weixin.qq.com

因此,在您的情况下,我认为您需要使用https和域api.wechat.com,如下所示:

response = requests.post('https://api.wechat.com/cgi-bin/media/upload', params=params, files=files)
相关问题