【问题标题】:Unable to update github file using CRUD API (using github-flask/python)无法使用 CRUD API 更新 github 文件(使用 github-flask/python)
【发布时间】:2014-11-08 22:20:25
【问题描述】:

我无法使用 github-flask 更新文件。

它给了我一个 404 错误 (https://developer.github.com/v3/troubleshooting/#why-am-i-getting-a-404-error-on-a-repository-that-exists) 但我已经验证我的身份验证令牌是好的并且具有回购范围。我是这样验证的

Anshu-MacBook-Pro:~ anshup$ curl -H "Authorization: token mytoken" https://api.github.com/users/anshprat -I
HTTP/1.1 200 OK
Server: GitHub.com
Date: Fri, 11 Jul 2014 07:21:34 GMT
Content-Type: application/json; charset=utf-8
Status: 200 OK
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4991
X-RateLimit-Reset: 1405065424
Cache-Control: private, max-age=60, s-maxage=60
Last-Modified: Fri, 11 Jul 2014 06:25:54 GMT
ETag: "etag"
X-OAuth-Scopes: repo

我的代码是:

def git_push(file_path,file_path_s,commit_message,raw_file_data):
    u_file_data = base64.b64encode(raw_file_data,salt())
    #print u_file_data
    params = {
            'path':file_path_s
            'message':commit_message,
            'content':u_file_data,
            'sha':file_sha  #Its a global variable
        }
    #print raw_file_data,u_file_data,params
    params_json = json.dumps(params)
    return github.post(file_path,params_json) #params fails as well

flask 帧转储显示:

resource    'repos/anshprat/git-test/contents/play1.txt'

status_code '404'

self    <flask_github.GitHub object at 0x2733b90>

response    <Response [404]>

kwargs  {'headers': {'Content-Type': 'application/x-www-form-urlencoded'}, 'data': '"{\\"content\\": \\"dXNlcjE6cGFzc3dvcmQxCnVzZXIyOnBhc3N3b3JkMgp1c2VyMzpNQlpvRDJLOWJQQS9vCg==\\", \\"sha\\": \\"ce4979fccba7ef259910c355ca6e1993d7ea436c\\", \\"message\\": \\"modifying user user3\\"}"'  }

method  'POST'

如果我在帖子中使用参数,双引号不会被转义。 POST 在 curl 上对我来说也失败了,所以我认为我在这里做了一些非常基本的错误?

     curl -H "Authorization: token mytoken" --data  '{"content": "dXNlcjE6cGFzc3dvcmQxCnVzZXIyOnBhc3N3b3JkMgp1c2VyMzpEQXR2VUNweUJrbjdFCg==", "message": "modifying user user3", "sha": "ce4979fccba7ef259910c355ca6e1993d7ea436c"}' https://api.github.com/repos/anshprat/git-test/contents/play1.txt
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3"
}

github.get 对于相同的 file_path 有效。

API 列于

https://developer.github.com/v3/repos/contents/#update-a-file

我的应用应该进行代码提交,然后将其部署到其他一些与 github 无关的工作。

【问题讨论】:

  • 看起来问题出在 github-flask 库上。我在stackoverflow.com/questions/19888832/… 中的实现之后对其进行了一些修改,现在似乎更好了。经过更多测试后,将在此处更新解决方案和对库的拉取请求,

标签: python github flask


【解决方案1】:

我不得不改变

def post(self, resource, data, **kwargs):
    """Shortcut for ``request('POST', resource)``.
    Use this to make POST request since it will also encode ``data`` to
    'application/x-www-form-urlencoded' format."""
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}

def post(self, resource, data, **kwargs):
    """Shortcut for ``request('POST', resource)``.
    Use this to make POST request since it will also encode ``data`` to
    'application/x-www-form-urlencoded' format."""
    headers = {}
    headers['Content-type'] = 'application/json'
    headers['X-HTTP-Method-Override'] = 'PUT'

因为它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多