1
我一直想写信给使用Python urllib2
与要点如下:发布与Github的API要旨
def _log_error(information, date=datetime.date.today(), current_time=time.strftime("%H:%M:%S")):
log_string = """
Info: {}
Date: {}
Time: {}
""".format(information, date, current_time)
filename = "<file>"
token = "<token>"
access_url = "https://api.github.com/gists/{}".format(filename)
req = urllib2.Request(access_url)
req.add_header("Authorization", "token {}".format(token))
req.add_header("Content-Type", "application/json")
json_data = {"content": log_string}
urllib2.urlopen(req, data=json.dumps(json_data))
然而,每次我尝试这样做,我得到以下错误:
Traceback (most recent call last):
File "printer.py", line 324, in <module>
_log_error("test")
File "printer.py", line 69, in _log_error
urllib2.urlopen(req, data=json.dumps(json_data))
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 435, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 473, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 422: Unprocessable Entity
是什么原因造成这个错误,我怎么能解决这个问题,而无需使用外部库(如requests
)?
这会在每次运行时创建一个要点...我不想继续创建一个,我想编辑sa我一遍又一遍地看着 – age97701
https://developer.github.com/v3/gists/#edit-a-gist。它需要参数中的要点'id'而不是'filename' –