githubgithub-api-v3

Using GitHub REST APIv3 for global code search


I am new to REST API. I am trying to create a query to search all the java repositories which contains com.jcabi.http.response.JsonResponse in its code.

I created following request by looking at the example given in the API documentation and my query throws an error.

This is the query:

curl https://api.github.com/search/code?q=com.jcabi.http.response.JsonResponse+language:java

It results in the following error:

Error 
{
  "message": "Validation Failed",
  "errors": [
    {
      "message": "Must include at least one user, organization, or repository",
      "resource": "Search",
      "field": "q",
      "code": "invalid"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/search/"
}

Is it impossible to do a code search without specifying a repo?

Can anybody help me regarding this?


Solution

  • You need to authenticate in order to access some paths, create a personal access token and then access the path using it.

    You can create a personal access token in settings -> Developer Settings -> Personal Access Token -> Generate New Token

    Copy that token to a safe place as it will be visible only for a session.

    Then use that token like this if you are using ajax calls

    https://api.github.com/search/code?q=com.jcabi.http.response.JsonResponse+language:java&access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    

    curl command for the same

    curl -v -H "Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxx" https://api.github.com/search/code?q=com.jcabi.http.response.JsonResponselanguage:java
    

    Note: Personal Access Tokens should be kept safe, don't share it to others, don't keep it in client side