Created
May 4, 2019 12:21
-
-
Save gsilos/77109ba9afdde584e6e79e4cfc1c24f2 to your computer and use it in GitHub Desktop.
OAuth1 in bash
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 | |
# oauth1 simulator http://lti.tools/oauth/ | |
urlencode () { | |
[ -n "$1" ] \ | |
&& echo -n "$1" | perl -p -e 's/([^A-Za-z0-9-._~])/sprintf("%%%02X", ord($1))/seg' | |
} | |
#oauth1 vars | |
consumer_key="key" | |
consumer_secret="secret" | |
token= | |
token_secret= | |
signature_method='HMAC-SHA1' | |
timestamp=$(date +%s) | |
#timestamp=1556970530 | |
#linux | |
#nonce=$(md5sum <<< "$RANDOM-$(date +%s.%N)" | cut -d' ' -f 1) | |
#mac | |
nonce=$(md5 <<< "$RANDOM-$(gdate +%s.%N)" | cut -d' ' -f 1) | |
#nonce=1 | |
url="http://localhost:5000/api" | |
encodedURL=$(urlencode $url) | |
PARAM=$(urlencode "oauth_consumer_key=$consumer_key&oauth_nonce=$nonce&oauth_signature_method=$signature_method&oauth_timestamp=$timestamp&oauth_token=$token&oauth_version=1.0") | |
signature=$(echo -n 'POST&'$URL'&'$PARAM|openssl dgst -sha1 -binary -hmac "$consumer_secret&$token_secret" |base64) | |
encoded_signature=$(urlencode $signature) | |
curl -v -X POST \ | |
$url \ | |
-H "Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_token=\"$token\",oauth_signature_method=\"$signature_method\",oauth_timestamp=\"$timestamp\",oauth_nonce=\"$nonce\",oauth_version=\"1.0\",oauth_signature=\"$encoded_signature\"" \ | |
-H 'Content-Type: application/json' \ | |
-H 'cache-control: no-cache' \ | |
-d '{ "key" : "value" } ' |
Thank you for this one. I've successfully sent a request to Twitter API. Just a correction, in
signature
calculation, replace $URL with $encodedURL in the echo command.
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this one. I've successfully sent a request to Twitter API.
Just a correction, in
signature
calculation, replace $URL with $encodedURL in the echo command.