Skip to content

Instantly share code, notes, and snippets.

@ccashwell
Created November 8, 2012 22:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ccashwell/4042214 to your computer and use it in GitHub Desktop.
Save ccashwell/4042214 to your computer and use it in GitHub Desktop.
Bash Scripting: OAUTH2 Authorization, Generating SSH keys and Adding Keys to GitHub
#!/bin/bash
# This is an example of OAUTH2 authorization via GitHub and adding a new (optionally generated) SSH key to your account.
# If generated, the key is put in the default location with no password. Security is obviously relaxed for brevity's sake. Enjoy.
read -p "GitHub Username: " uname
read -s -p "GitHub Password: " passwd
if [[ "$uname" == "" || "$passwd" == "" ]]; then
echo -e "\n\nCan't set up your GitHub SSH keys without authorization. You're on your own now, champ."
exit 1
fi
token=$(curl -u $uname:$passwd --silent -d '{"scopes":["user"]}' "https://api.github.com/authorizations" | grep -o '[0-9A-Fa-f]\{40\}')
echo -e "\n"
read -p "Generate new (and backup any current) SSH keys? (y):" createkey
if [[ "${createkey:=y}" == "y" ]]; then
echo -e "Generating new SSH keys...\n"
read -p "Enter your email address: " email
mkdir -p ~/.ssh/key_backup && mv ~/.ssh/id_rsa* ~/.ssh/key_backup
echo -e "\n\n\n" | ssh-keygen -t rsa -N "" -C ${email:=null@example.com}
fi
sshkey=`cat ~/.ssh/id_rsa.pub`
curl -X POST -H "Content-type: application/json" -d "{\"title\": \"$email\",\"key\": \"$sshkey\"}" "https://api.github.com/user/keys?access_token=$token"
echo -e "\nAll set, now get to work!"
exit 0
@golokeshpatra45
Copy link

I am getting this error ,

{
"message": "Bad credentials",
"documentation_url": "https://developer.github.com/v3"
}

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