8

Hi I'm currently trying to decode the base64 returned from the Github API /:username/:repo/contents/:filepath

which returns a json object

{ "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" } }

The content is encoded as base64, but when I try to decode it - it gives me random characters

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

here is my code:

   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

1 Answer 1

6

Parse the json response with json.loads(result.content) and get the content field afterwards :

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)

You can test it here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.