0

I am trying to retrieve an xml file from the github using python program. I am trying different url but for each of them it say content not found. What are different options to retrieve the file from github? Any help on this would be much appreciated!!

import requests
from github import Github
import base64

g = Github("access_token")

#url = 'https://api.github.com/repos/{username}/{repos_name}/contents/{path}/{filename}.xml'
#url = 'https://git.<company domain>.com/raw/IT/{repos_name}/{path}/{filename}.xml?token<value>'
url = 'https://git.<company domain>.com/raw/IT/{repos_name}/{path}/{filename}.xml?token=<value>'
req = requests.get(url)

#print ('Keep Going!', req.content)

if req.status_code == requests.codes.ok:
    req = req.json()  
`. `# the response is a JSON
    # req is now a dict with keys: name, encoding, url, size ...
    # and content. But it is encoded with base64.
    content = base64.decodestring(req['content'])
else:
    print('Content was not found.')

output:

Keep Going! b'{"message":"Not Found","documentation_url":"https://developer.github.com/v3/repos/contents/#get-contents"}'
Content was not found.
MikaelF
  • 3,518
  • 4
  • 20
  • 33
  • Based on the output it seems as though there could be an issue with the destination. Are you certain that is not the case. – Michael Nelles Apr 05 '20 at 00:43
  • `repos_name`, `path`, `filename`, ... are never formatted and are not declared anywhere in your code. – Andrea Corbellini Apr 05 '20 at 02:00
  • That's what I am trying to figure. What is the best way to get correct url from github? I tried to put normal url, url from "raw" selection but none of them are working. – hellofriends_newpy Apr 05 '20 at 03:52
  • @andrea - I have replaced the actual name with placeholder for confidentiality. While running the code, I am passing repos_name, path, filename with actual values – hellofriends_newpy Apr 05 '20 at 03:54
  • It's hard to tell where the problem could be without seeing the full URL. I'd suggest trying with public repos, e.g. are you able to use your code to retrieve [this file](https://api.github.com/repos/python/cpython/contents/README.rst)? Also, I'm not a GitHub expert, but it seems to me that [authentication is only supported via headers](https://developer.github.com/v3/#authentication)--you're passing the token as an URL parameter instead, and I think that's not supported – Andrea Corbellini Apr 05 '20 at 05:37
  • Found also [this article about authentication](https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/). Note that it mentions `access_token`, and not `token`. Maybe if you change your code to use `access_token` it would work? – Andrea Corbellini Apr 05 '20 at 05:44
  • This has been answered here: https://stackoverflow.com/a/14120601/2052575 – v25 Apr 05 '20 at 10:32

1 Answers1

0

Replace all the <> and {} variable in your url with the actual path to the file you're trying to retrieve.