使用Github API发布到Gist

时间:2017-04-20 18:01:09

标签: python python-2.7 github-api gist

我一直在尝试使用Python urllib2写一篇关于gist的要点:

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)?

2 个答案:

答案 0 :(得分:1)

要创建gist,请使用Create Gist端点需要以下JSON格式:

{
  "description": "the description for this gist",
  "public": true,
  "files": {
    "file1.txt": {
      "content": "String file contents"
    }
  }
}

以下内容将映射descriptionpublicfilename和您的3个内容字段infodatecurrent_time的正确字段:

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)

答案 1 :(得分:0)

我正在维护一个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方法中获取主ID
  • 更改说明

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -cd "New description"
      您可以从之前的get方法中获取主ID
  • nano vim gedit 等编辑器中交互式地编辑文件内容/ p>

    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md
  • 同时做
    • gifc update ffd2f4a482684f56bf33c8726cc6ae63 -f file_to_update.md -cd "New description"