从 Github API 解码 base64 内容

Sea*_*gel 5 python web2py github github-api google-cloud-platform

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

它返回一个 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
Run Code Online (Sandbox Code Playgroud)

Ber*_*tel 5

解析 json 响应并随后json.loads(result.content)获取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)
Run Code Online (Sandbox Code Playgroud)

你可以在这里测试一下