Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Created April 24, 2020 13:14
Show Gist options
  • Save cibofdevs/eb953b59166247d6f73a59d340146009 to your computer and use it in GitHub Desktop.
Save cibofdevs/eb953b59166247d6f73a59d340146009 to your computer and use it in GitHub Desktop.

Introduction

An introduction to curl using GitHub's API

The Basics

Makes a basic GET request to the specifed URI

curl https://api.github.com/users/cibofdevs

Includes HTTP-Header information in the output

curl --include https://api.github.com/users/cibofdevs

Pass user credential to basic auth to access protected resources like a users starred gists, or private info associated with their profile

curl --user "cibofdevs:PASSWD" https://api.github.com/gists/starred
curl --user "cibofdevs:PASSWD" https://api.github.com/users/cibofdevs

Passing just the username without the colon(:) will cause you to be prompted for your account password. This avoids having your password in your command line history

curl --user "cibofdevs" https://api.github.com/users/cibofdevs

POST

Use the --request (-X) flag along with --data (-d) to POST data

curl --user "cibofdevs" --request POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

curl --user "cibofdevs" -X POST --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

Of course --data implies POST so you don't have to also specify the --request flag

curl --user "cibofdevs" --data '{"description":"Created via API","public":"true","files":{"file1.txt":{"content":"Demo"}}' https://api.github.com/gists

Here is an example that uses the old GitHub API (v2). You can use multiple --data flags

curl --data "login=cibofdevs" --data "token=TOKEN" https://github.com/api/v2/json/user/show/cibofdevs

The post data gets combined into one so you can also just combine them yourself into a single --data flag

curl --data "login=cibofdevs&token=TOKEN" https://github.com/api/v2/json/user/show/cibofdevs

You can tell curl to read from a file (@) to POST data

curl --user "cibofdevs" --data @data.txt https://api.github.com/gists 

Or it can read from STDIN (@-)

curl --user "cibofdevs" --data @- https://api.github.com/gists
{
  "description":"Test",
  "public":false,
  "files": {
    "file1.txt": {
      "content":"Demo"
    }
  }
}
end with ctrl+d

OAuth

The first thing to know is that your API Token (found in https://github.com/settings/admin) is not the same token used by OAuth. They are different tokens and you will need to generate an OAuth token to be authorized.

Follow the API's instructions at http://developer.github.com/v3/oauth/ under the sections "Non-Web Application Flow" and "Create a new authorization" to become authorized.

Note: Use Basic Auth once to create an OAuth2 token http://developer.github.com/v3/oauth/#oauth-authorizations-api

curl https://api.github.com/authorizations \
--user "cibofdevs" \
--data '{"scopes":["gist"],"note":"Demo"}'

This will prompt you for your GitHub password and return your OAuth token in the response. It will also create a new Authorized application in your account settings https://github.com/settings/applications

Now that you have the OAuth token there are two ways to use the token to make requests that require authentication (replace "OAUTH-TOKEN" with your actual token)

curl https://api.github.com/gists/starred?access_token=OAUTH-TOKEN
curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com/gists/starred

List the authorizations you already have

curl --user "cibofdevs" https://api.github.com/authorizations

Resources

@gitezri
Copy link

gitezri commented Mar 21, 2021

Useful. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment