【问题标题】:Decoding base64 content from Github API从 Github API 解码 base64 内容
【发布时间】:2018-12-19 00:12:27
【问题描述】:

嗨,我目前正在尝试解码从 Github API /:username/:repo/contents/:filepath 返回的 base64

返回一个json对象

{ "type": "file", "encoding": "base64", "size": 5362, "name": "README.md", "path": "README.md", "content": "encoded content ...", "sha": "3d21ec53a331a6f037a91c368710b99387d012c1", "url": "https://api.github.com/repos/octokit/octokit.rb/contents/README.md", "git_url": "https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6>f037a91c368710b99387d012c1", "html_url": >"https://github.com/octokit/octokit.rb/blob/master/README.md", "download_url": >"https://raw.githubusercontent.com/octokit/octokit.rb/master/README.md", "_links": { "git": "https://api.github.com/repos/octokit/octokit.rb/git/blobs/3d21ec53a331a6f037a91c368710b99387d012c1", "self": "https://api.github.com/repos/octokit/octokit.rb/contents/README.md", "html": "https://github.com/octokit/octokit.rb/blob/master/README.md" } }

内容被编码为 base64,但当我尝试解码时 - 它给了我随机字符

from googleapiclient import discovery from goйɽ́Ё!ɽ)ɽwWF&6ƖVB6W'f6U66VB'B6W'f6T66[ܙY[X[[\ܝX]QgTS_DISCOVERY_URL='https://sheets.googleapis.c͍ٕɕٕͥМ)M.....

这是我的代码:

   try:
       result = urlfetch.fetch(url, headers={"accept": >"application/vnd.github.v3+json"})
       if result.status_code == 200:
           decoded_content = base64.b64decode(result.content)
           print(decoded_content)
        else:
                   self.response.status_code = result.status_code

【问题讨论】:

    标签: python github google-cloud-platform web2py github-api


    【解决方案1】:

    使用json.loads(result.content) 解析json 响应,然后获取content 字段:

    import base64
    import urlfetch
    import json
    
    result = urlfetch.fetch(
        "https://api.github.com/repos/octokit/octokit.rb/contents/README.md", 
        headers={"accept": "application/vnd.github.v3+json"})
    
    if result.status_code == 200:
        data = json.loads(result.content)
        decoded_content = base64.b64decode(data["content"])
        print(decoded_content)
    else:
        print(result.status_code)
    

    你可以测试一下here

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 2017-04-07
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 2015-10-09
      相关资源
      最近更新 更多