CodeBlanks Logo Icon CodeBlanks

Authentication

Learn token-based authentication.

GitHub API authentication

The GitHub API allows access to some public resources without authentication. Authentication is required to access private resources or benefit from higher request limits.

Authorization: Bearer YOUR_TOKEN

A token is used to authenticate your identity in an API request. You should never add token values to public repositories or source code.

Using a Personal Access Token

A Personal Access Token (PAT) allows you to perform API operations on behalf of your GitHub account with specific permissions. Granting only the required permissions when creating a token is important for security.

curl -H "Authorization: Bearer $GITHUB_TOKEN" \
  https://api.github.com/user

Keeping the token in an environment variable is safer than writing it directly into a script. If a token is exposed, revoke its access and create a new one.

Practice Request user information with a token

Use the GITHUB_TOKEN environment variable to send a request to the GitHub user endpoint. Use Bearer token authentication and the GET method. Print the HTTP response status code.

Starter code

terminal
# Token ile kimlik doğrulaması yaparak kullanıcı bilgilerini iste
# HTTP durum kodunu göster

Sample solution

terminal
curl -sS -o /dev/null -w "%{http_code}
" -X GET -H "Authorization: Bearer $GITHUB_TOKEN" https://api.github.com/user

Expected output

200

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which HTTP header is used to securely send a personal access token in an API request?

  • Accept-Language
  • User-Agent
  • Content-Type
  • Authorization Correct answer

Explanation: The <code>Authorization</code> header carries token-based authentication credentials.

2 Define the PAT variable

Question
export GITHUB_TOKEN=ghp_example_token____
Answer
export GITHUB_TOKEN=ghp_example_tokenGITHUB_TOKEN

Explanation: You can store the token value in the GITHUB_TOKEN environment variable.

3 Token security concepts

Term Meaning
PAT Personal access token
Bearer Token transmission scheme
Scope Token permission scope
Environment variable Stores a secret value in the environment

All lessons in Expert