The library of Iotashan doesn’t work correct with Extended UTF-8 characters. If you use this characters (for example, characters of the Russian language) in the parameters and his class URLEncoding you’ll receive error of invalid signature.
In one of the comments of his blog was suggested that the use of class URLEncoding to use the function encodeURIComponent. However, in this case, the same error was received during the use of some special characters (‘!’, ‘*’, perhaps some others).
The solution of this error was the implementation of three methods:
public static function utf8Encode(string:String):String { string = string.replace(/\r\n/g,'\n'); string = string.replace(/\r/g,'\n'); var utfString:String = ''; for (var i:int = 0 ; i < string.length ; i++) { var chr:Number = string.charCodeAt(i); if (chr < 128) { utfString += String.fromCharCode(chr); } else if ((chr > 127) && (chr < 2048)) { utfString += String.fromCharCode((chr >> 6) | 192); utfString += String.fromCharCode((chr & 63) | 128); } else { utfString += String.fromCharCode((chr >> 12) | 224); utfString += String.fromCharCode(((chr >> 6) & 63) | 128); utfString += String.fromCharCode((chr & 63) | 128); } } return utfString; } public static function urlEncode(string:String):String { var urlString:String = ''; for (var i:int = 0 ; i < string.length ; i++) { var chr:Number = string.charCodeAt(i); if ((chr >= 48 && chr <= 57) || // 09 (chr >= 65 && chr <= 90) || // AZ (chr >= 97 && chr <= 122) || // az chr == 45 || // - chr == 95 || // _ chr == 46 || // . chr == 126) // ~ { urlString += String.fromCharCode(chr); } else { urlString += '%' + chr.toString(16).toUpperCase(); } } return urlString; } public static function encode(string:String):String { return urlEncode(utf8Encode(string)); }
Then when you replace all the methods used in the library URLEncoding.encode above a certain method encode the specified error no longer occurs.
Helped to understand what an error Beginner’s Guide to OAuth – Part IV: Signing Requests, and a script guide4.js.
UPD:
The new version now works correctly with extended characters UTF-8. Thank you Shannon Hicks for a quick update. And also, thank you for such a cool library.
UPD:
To view full code of class, please, look at this file.
I just added your changes into the source. Thanks for contributing!
string = string.replace(/r/g,’n');
This changed all my ‘r’s to ‘n’s
oh… yeah… Thanks for this comment…
For some reason the character «\» was not displayed in my post. In particular, instead of the string
string = string.replace(/r/g,’n');need to be a string
string = string.replace(/\r/g,'\n');.Please, look at this file
Just curious as to whether anyone’s had any problems using this code to with line breaks on Windows; it doesn’t seem to work for me — I keep getting invalid-signature responses from Twitter, for instance.
I believe it is still not working correctly. For example letter «č» is translated into
C4 3F
but it should be
C4 8D