我正在bakery项目中使用Flask(想法类似于travisci,但是用python完成的)。以及为拥有授权github用户的存储库创建添加webhook的模块。我没有在这里粘贴完整的示例代码,这里是我要做的事情的片段。完整示例可在separate gist中找到。在
问题是Github API GET请求正常工作。但同时返回responce体中的401和{}。在# this method is working
@app.route('/repos')
def repos():
# only logged in user should call it, but I skip it in this example
resp = github.get('/user/repos', data = {'type': 'public'})
# responce status code is ok, and data is returned
print(resp.data) # should print to console
@app.route('/addhook/')
def repos(full_name):
HOOK_URL = 'http://example.com/hook'
resp = github.post('/repos/%(full_name)s/hooks' % {'full_name': full_name},
data = {
'name':'web',
'active': True,
'events': ['push'],
'config': {
'url': HOOK_URL,
'content_type': 'json'
}
},
format = 'json'
)
# POST request is not working and form request that Github is not understand
print(resp.status, resp.data)
我检查了哪些header generate Flask OAuth,发现它添加了额外的标题行,其中包含以下内容(跳过实际值):
^{pr2}$
结果Github返回这个状态,可能不知道该怎么处理。我找到了解决方法,如果我只复制token并直接调用Github API,就可以得到预期的结果。这里的等价叫法是curl:$ curl https://api.github.com/repos/xen/league-gothic/hooks?access_token=XXX -X POST --data '{"name":"web","active":true,"events": ["push"],"config": {"url": "http://example.com/hook","content_type": "json"}}'
所以,问题是:有没有可能让它看起来更好,并使用flaskoauth方法POST来简化代码并使其正常工作?在