Python 使用Github API发布到Gist
我一直在尝试使用Python urllib2编写要点,包括以下内容:Python 使用Github API发布到Gist,python,python-2.7,github-api,gist,Python,Python 2.7,Github Api,Gist,我一直在尝试使用Python urllib2编写要点,包括以下内容: 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) filena
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
导致此错误的原因是什么?如何在不使用外部库(如请求)的情况下修复此错误?要创建使用端点,需要以下JSON格式:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
下面将映射描述、公共、文件名和3个内容字段信息、日期和当前时间的正确字段:
要创建使用端点,需要以下JSON格式:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
下面将映射描述、公共、文件名和3个内容字段信息、日期和当前时间的正确字段:
我有一个Gist客户机,它可以满足您的需求,也可以满足您在评论中的要求。您可以从中克隆它并通过pip进行安装。 如您所述,创建和更新GIST时使用的简要摘要- 提出要点 从nano、vim或gedit等编辑器以交互方式创建 gifc create create.md-d如何从cli-i nano创建要点 直接从cli输入内容 gifc create create.md-d如何从cli创建要点-m'如果要从现有文件创建要点,请执行以下操作-`./gifc-c create.md-e如何从cli-i file.md`' 从文件中获取内容 gifc create create.md-d如何从cli-f file.md创建要点 更新要点 以迭代方式编辑所有或部分文件 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-i vi 您可以从前面的get方法中获取gist id 更改描述 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-cd新说明 您可以从前面的get方法中获取gist id 在nano、vim或gedit等编辑器中以交互方式编辑文件内容 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-f文件_to_update.md 兼而有之 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-f文件_to_update.md-cd新说明
我有一个Gist客户机,它可以满足您的需求,也可以满足您在评论中的要求。您可以从中克隆它并通过pip进行安装。 如您所述,创建和更新GIST时使用的简要摘要- 提出要点 从nano、vim或gedit等编辑器以交互方式创建 gifc create create.md-d如何从cli-i nano创建要点 直接从cli输入内容 gifc create create.md-d如何从cli创建要点-m'如果要从现有文件创建要点,请执行以下操作-`./gifc-c create.md-e如何从cli-i file.md`' 从文件中获取内容 gifc create create.md-d如何从cli-f file.md创建要点 更新要点 以迭代方式编辑所有或部分文件 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-i vi 您可以从前面的get方法中获取gist id 更改描述 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-cd新说明 您可以从前面的get方法中获取gist id 在nano、vim或gedit等编辑器中以交互方式编辑文件内容 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-f文件_to_update.md 兼而有之 gifc更新ffd2f4a482684f56bf33c8726cc6ae63-f文件_to_update.md-cd新说明
这会在每次运行时创建一个要点。。。我不想继续创建一个,我想一遍又一遍地编辑同一个。它需要参数中的gist id而不是文件名这会在每次运行时创建gist。。。我不想继续创建一个,我想一遍又一遍地编辑同一个。它需要参数中的gist id,而不是文件名