Created
November 8, 2012 22:29
-
-
Save ccashwell/4042214 to your computer and use it in GitHub Desktop.
Bash Scripting: OAUTH2 Authorization, Generating SSH keys and Adding Keys to GitHub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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:[email protected]} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am getting this error ,
{
"message": "Bad credentials",
"documentation_url": "https://developer.github.com/v3"
}