GitHub recently deprecated password-based authentication for its APIs. Instead, they recommend creating a personal access token and using it as an authentication token for API requests.
I have a new project where I’m querying GitHub for repo data, so I need to use the API. I created a personal access token and stored it in my OSX keychain using the command:
security add-generic-password \
-s "summon" \
-a "github/api_token" \
-w "[ACCESS TOKEN]"
Now, whenever I need to send a request, I can use Summon to inject the token into the request header as follows:
summon -p keyring.py \
--yaml 'TOKEN: !var github/api_token' \
bash -c '
curl -H "Authorization: token $TOKEN" https://api.github.com
'
This is great, because I have a safe place to keep my token and I don’t have to copy/paste it to inject it into my GitHub API request.
For now I’m just copy/pasting this snippet and changing the request URL as needed, but if I need to do this often enough I may define a github_api
method in my .bashrc
that takes in an API route and runs the command above. That might look something like:
function github_api() {
local route=$1
summon -p keyring.py \
--yaml 'TOKEN: !var github/api_token' \
bash -c '
curl -H "Authorization: token $TOKEN" https://api.github.com/'$route
}
I hope you find this useful too!