从 Github API 解码 base64 内容

Decoding base64 content from Github API

您好,我目前正在尝试解码从 Github API /:username/:repo/contents/:filepath

返回的 base64

哪个returns一个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

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