使用 Github API 发布到 Gist
Posting to a Gist with Github API
我一直在尝试使用 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)
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
)的情况下修复它?
要创建 gist , use Create Gist 端点需要以下 JSON 格式:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
以下将为 description
、public
、filename
和您的 3 个内容字段 info
、date
和 [=17 映射正确的字段=]:
import urllib2
import json
import datetime
import time
token = "YOUR_TOKEN"
access_url = "https://api.github.com/gists"
filename = "file.txt"
description = "the description for this gist"
public = "true"
information = "some info"
date = datetime.date.today()
current_time = time.strftime("%H:%M:%S")
data = """{
"description": "%s",
"public": %s,
"files": {
"%s": {
"content": "info : %s,date: %s, current_time: %s"
}
}
}"""
json_data = data % (description, public, filename, information, date, current_time)
req = urllib2.Request(access_url)
req.add_header("Authorization", "token {}".format(token))
req.add_header("Content-Type", "application/json")
urllib2.urlopen(req, data=json_data)
我正在维护一个 Gist 客户端,它可以完全满足您的需求以及您在评论中提出的要求。您可以从 here.
克隆它并通过 pip 安装它
如您所述,创建和更新要点时的用法简要总结 -
创建要点
- 从 nano、vim 或 gedit 等编辑器交互式创建
gifc create create.md -d "How to create a gist from cli" -i nano
- 直接从cli输入内容
gifc create create.md -d "How to create a gist from cli" -m '''If you want to create a gist from an existing file then you do the following- `./gifc -c create.md -e "How to create a gist from cli" -i file.md`'''
- 从文件中获取内容
gifc create create.md -d "How to create a gist from cli" -f file.md
更新要点
迭代编辑所有(或部分)文件
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
您可以从早期的 get
方法中获取 gist id
更改描述
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
您可以从早期的 get
方法中获取 gist id
在 nano、vim[=88 等编辑器中交互式编辑文件内容=] 或 gedit
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
- 两者都做
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"
我一直在尝试使用 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)
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
)的情况下修复它?
要创建 gist , use Create Gist 端点需要以下 JSON 格式:
{
"description": "the description for this gist",
"public": true,
"files": {
"file1.txt": {
"content": "String file contents"
}
}
}
以下将为 description
、public
、filename
和您的 3 个内容字段 info
、date
和 [=17 映射正确的字段=]:
import urllib2
import json
import datetime
import time
token = "YOUR_TOKEN"
access_url = "https://api.github.com/gists"
filename = "file.txt"
description = "the description for this gist"
public = "true"
information = "some info"
date = datetime.date.today()
current_time = time.strftime("%H:%M:%S")
data = """{
"description": "%s",
"public": %s,
"files": {
"%s": {
"content": "info : %s,date: %s, current_time: %s"
}
}
}"""
json_data = data % (description, public, filename, information, date, current_time)
req = urllib2.Request(access_url)
req.add_header("Authorization", "token {}".format(token))
req.add_header("Content-Type", "application/json")
urllib2.urlopen(req, data=json_data)
我正在维护一个 Gist 客户端,它可以完全满足您的需求以及您在评论中提出的要求。您可以从 here.
克隆它并通过 pip 安装它
如您所述,创建和更新要点时的用法简要总结 -
创建要点
- 从 nano、vim 或 gedit 等编辑器交互式创建
gifc create create.md -d "How to create a gist from cli" -i nano
- 直接从cli输入内容
gifc create create.md -d "How to create a gist from cli" -m '''If you want to create a gist from an existing file then you do the following- `./gifc -c create.md -e "How to create a gist from cli" -i file.md`'''
- 从文件中获取内容
gifc create create.md -d "How to create a gist from cli" -f file.md
更新要点
迭代编辑所有(或部分)文件
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -i vi
您可以从早期的get
方法中获取 gist id
更改描述
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
您可以从早期的get
方法中获取 gist id
在 nano、vim[=88 等编辑器中交互式编辑文件内容=] 或 gedit
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
- 两者都做
gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"