Search code examples
github-api

Find all github repositories based on a specific keyword


I am trying to find all repos based on a keyword 'TERRAGRUNT_VERSION' using github search api and its returning the below error. Am I missing something in the url? https://docs.github.com/en/rest/reference/search#search-repositories

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 3 column 1 (char 2)

Code:

import requests


token = "access_token=" + "23456789087" 
base_api_url = 'https://api.github.com/'
search_final_url = base_api_url + 'search/repositories?q=TERRAGRUNT_VERSIONin:env/Makefile' + '&' + token       

try:
  response = requests.get(search_final_url).json()
  for item in response['items']:
     repo_name = item['name']
     print(repo_name)  
except Exception as e:
  print(e)
  

The issue is I'm getting a 400 response. I tried the below

response = requests.get("https://api.github.com/search/repositories?q=TERRAGRUNT_VERSIONin:env/Makefile&access_token=456744")
print(response)

Output:
<Response [400]>
{'message': 'Must specify access token via Authorization header. https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param', 'documentation_url': 'https://docs.github.com/v3/#oauth2-token-sent-in-a-header'}

Solution

  • In short, you have to pass access_token not as the url param but through the request header

    If you try to search your link with browser, you would get the answer

    {
      "message": "Must specify access token via Authorization header. https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param",
      "documentation_url": "https://docs.github.com/v3/#oauth2-token-sent-in-a-header"
    }
    

    Full description is on the link from response