Last active
January 27, 2017 23:29
-
-
Save yhpark/fdc71cb8ece304839f89de6592649b90 to your computer and use it in GitHub Desktop.
OAuth 1.0a signature base string with form data
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
function percentEncode(string) { | |
var char, charCode, i; | |
var encodedString = ''; | |
for (i=0; i<string.length; i++) { | |
char = string.charAt(i); | |
if ((char >= '0' && char <= '9') || | |
(char >= 'A' && char <= 'Z') || | |
(char >= 'a' && char <= 'z') || | |
(char == '-') || (char == '.') || | |
(char == '_') || (char == '~')) { | |
encodedString += char; | |
} else { | |
charCode = string.charCodeAt(i); | |
encodedString += '%' + charCode.toString(16).toUpperCase(); | |
} | |
} | |
return encodedString; | |
}; | |
var dat = ''; // your (unparsed) form data here | |
'{method}&{url}&' + percentEncode(dat.split("&") | |
.map(a => a.split("=") | |
.map(b => percentEncode(decodeURIComponent(b.replace(/\+/g,' '))))) | |
.filter(a => a[0] !== 'oauth_signature') | |
.sort((a, b) => { | |
if (a[0] < b[0]) return -1; | |
else if (a[0] == b[0]) return 0; | |
return 1; | |
}).map(a => a[0] + '=' + a[1]) | |
.join('&')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment