【问题标题】:Got 503 error code while retrieveing mediaid in wechat?在微信中检索医疗补助时出现503错误代码?
【发布时间】:2019-03-05 06:55:57
【问题描述】:

APIhttp://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)

【问题讨论】:

  • 输出是什么,错误是什么(如果有的话)?
  • @NiklasMertsch 得到 503 错误代码

标签: python python-requests wechat


【解决方案1】:

看起来您正在使用http 进行上传调用。由于这个原因,我之前已经看到网站返回此错误。

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

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

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

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

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

答:如果您有国际公众号,请使用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)

【讨论】:

猜你喜欢
  • 2020-10-10
  • 2021-08-01
  • 1970-01-01
  • 2015-03-24
  • 2013-09-06
  • 2022-06-14
  • 2019-10-06
  • 1970-01-01
相关资源
最近更新 更多