【问题标题】:Possible to request github JSON file without token可以在没有令牌的情况下请求 github JSON 文件
【发布时间】:2020-04-08 20:57:22
【问题描述】:

所以我在解决如何通过请求读取我的 JSON 格式的 repo 文件时遇到了一些问题。 (Python)

基本上我已经创建了一些简单的东西,例如:

r = requests.get('https://raw.githubusercontent.com/Test/testrepo/master/token.json?token=ADAJKFAHFAKNQ3RKVSUQ5T12333777777')

但是,每次我对该文件进行新的提交/更改时,它都会给我一个新的令牌,然后我需要重新编码。

所以我的问题是,是否可以在没有令牌的情况下访问 JSON 文件? (我也需要将 repo 保密),但关键是我希望能够在不更改 URL 的情况下对文件进行更改。

【问题讨论】:

    标签: python github


    【解决方案1】:

    最简单的解决方案可能是使用GitHub API,而不是尝试使用您在浏览器中看到的“原始”链接。

    1. 首先,获取personal access token
    2. 现在使用该访问令牌向 /repos 发出 API 请求:

      import requests
      token = "MY_SECRET_TOKEN"
      owner = 'Test'
      repo = 'testrepo'
      path = 'token.json'
      
      r = requests.get(
            'https://api.github.com/repos/{owner}/{repo}/contents/{path}'.format(
              owner=owner, repo=repo, path=path),
            headers={
              'accept': 'application/vnd.github.v3.raw',
              'authorization': 'token {}'.format(token),
            }
          )
      print(r.text)
      

    【讨论】:

      【解决方案2】:

      您可以使用Github python library 获取存储库中的任何文件。由于您提到将存储库保密,因此您必须使用here 描述的方法之一登录到 github。下面是一个使用github用户名和密码获取文件的例子

      from github import Github
      
      user_name = <YOUR_USERNAME>
      password = <YOUR_PASSWORD>
      g = Github(user_name, password)
      
      
      file_name='test.json' #Choose your required file name location
      repo_name = 'repo_name'
      repo_location = '{}/{}'.format(user_name, repo_name)
      
      repo = g.get_repo(repo_location)
      
      file = repo.get_contents(file_name)
      
      
      #if you want the download url for the file (this comes along with the token that you talked about earlier)
      download_url = file.download_url
      
      #if you simply want the content inside the file
      content = file.decoded_content
      

      【讨论】:

        【解决方案3】:

        @larsks provides solution很好,我想补充一下。


        我选择一个公共仓库awesome-python作为例子

        假设你想访问master/docs/CNAME的内容

        import requests
        token = "MY_SECRET_TOKEN"
        owner = 'vinta'
        repo = 'awesome-python'
        path = 'docs/CNAME'
        branch = 'master' # or sha1, for example: 6831740
        
        url = f'https://api.github.com/repos/{owner}/{repo}/contents/{path}?ref={branch}'
        print(url)
        r = requests.get(url,
              headers={
                'accept': 'application/vnd.github.v3.raw',
                # 'authorization': f'token {token}', # If you are want to read "public" only, then you can ignore this line.
              }
            )
        print(r.text)
        
        """
        Type
        r.text: str
        r.content: bytes
        """
        
        # If you want to save it as a file, then you can try as below.
        # f=open('temp.ico','wb')
        # f.write(r.content)
        

        但我认为很多人可能想要访问私有存储库。

        然后去

        1. github.com/settings/tokens

        2. 生成新令牌

        3. click repo(完全控制私有仓库)

        4. add header of authorizationcancel comment

        【讨论】:

          猜你喜欢
          • 2020-10-20
          • 2017-11-02
          • 1970-01-01
          • 2021-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-11
          • 2012-10-25
          相关资源
          最近更新 更多