Skip to content

Instantly share code, notes, and snippets.

@hypest
Last active July 10, 2017 16:02
Show Gist options
  • Save hypest/105aeef7bf863675956c to your computer and use it in GitHub Desktop.
Save hypest/105aeef7bf863675956c to your computer and use it in GitHub Desktop.
WordPress.com oAuth2 access token request via command line (cURL)
#!/bin/sh
echo oAuth2 app id:
read appid
echo oAuth2 app secret:
read -s appsecret
echo Username:
read username
echo Password:
read -s password
echo
function getJsonVal () {
python -c "import json,sys;sys.stdout.write(json.dumps(json.load(sys.stdin)$1))";
}
wp_access_token=$(curl -# -X POST --data-urlencode "grant_type=password" --data-urlencode "client_id=$appid" --data-urlencod
e "client_secret=$appsecret" --data-urlencode "username=$username" --data-urlencode "password=$password" https://public-a
pi.wordpress.com/oauth2/token | getJsonVal "['access_token']")
# remove the start and end quotes
wp_access_token="${wp_access_token%\"}"
wp_access_token="${wp_access_token#\"}"
export wp_access_token
echo New token acquired. If you sourced this script then variable \$wp_access_token has been set to: $wp_access_token
echo You may use it like this: curl --header \"Authorization: Bearer \$wp_access_token\" \<URL\>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment