Created
March 21, 2017 14:36
-
-
Save phcorp/0d93962a313c0cadd3eb0e2a98dd4de7 to your computer and use it in GitHub Desktop.
Bash oauth with curl on OSX
This file contains 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
#!/usr/bin/env bash | |
# parameters | |
url="https://example.com" | |
client_id="foo" | |
client_secret="bar" | |
# request access token | |
oauth_path="/oauth2/access_token" | |
access_token=$(curl -s -H "Accept: application/json" -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" -H "Authorization: Basic $(echo "$client_id:$client_secret" | tr -d "\n" | base64)" -X POST -d "grant_type=client_credentials" "$url$oauth_path" | sed -e 's/^.*"access_token":"\([^"]*\)".*$/\1/') | |
# call service using access token | |
service_path="/foo" | |
body="{}" | |
curl -s -H "Content-Type: application/json; charset=utf-8" -H "Authorization: Bearer $access_token" -X POST -d $body "$url$service_path" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment