Hello! Sorry for delay, we're working on updating tutorial. Looks like you have a few closing brackets missing. Please try create three separate JS files instead one file with the following content:
1) codeTwitSearchGlobal = {
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
bearerToken : "",
// public method for encoding
encode : function (input) {
var output = ""
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
Code: Select all
input = TwitSearchGlobal._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = ""
Code: Select all
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
Code: Select all
beforeSearchTwit : function () {
if (this.bearerToken == ""){ // get bearer token and after that call searchTwit
OAuth2DS.execute({});
} else {
SearchDS.execute({});
}
},
accessTokenExist : false,
handleError : function (message) {
$('[name=errLabel]').text(message);
},
goodMessage : function () {
$('[name=sendLabel]').text('Your message has been sent.');
},
clearLabels : function () {
$('[name=sendLabel]').text('');
$('[name=errLabel]').text('');
},
parseReply : function (reply){
try {
if (reply.indexOf('<!--?xml version') === 0){ // XML has got
this.handleError(reply.match(/(.*)<TwitSearchGlobal = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
bearerToken : "",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
Code: Select all
input = TwitSearchGlobal._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 2;
enc2 = ((chr1 & 3) << 4) | (chr2 4);
enc3 = ((chr2 & 15) << 2) | (chr3 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
Code: Select all
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c 127) && (c < 2048)) {
utftext += String.fromCharCode((c 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c 12) | 224);
utftext += String.fromCharCode(((c 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
Code: Select all
beforeSearchTwit : function () {
if (this.bearerToken == ""){ // get bearer token and after that call searchTwit
OAuth2DS.execute({});
} else {
SearchDS.execute({});
}
},
accessTokenExist : false,
handleError : function (message) {
$('[name=errLabel]').text(message);
},
goodMessage : function () {
$('[name=sendLabel]').text('Your message has been sent.');
},
clearLabels : function () {
$('[name=sendLabel]').text('');
$('[name=errLabel]').text('');
},
parseReply : function (reply){
try {
if (reply.indexOf('?xml version') === 0){ // XML has got
this.handleError(reply.match(/(.*)/)[1]);
return 1;
}
var array = reply.split('&');
var tmp = array[0].split('=',2);
EncodeHelper.token = unescape(tmp[1]);
tmp = array[1].split('=',2);
EncodeHelper.tokenSecret = unescape(tmp[1]);
return 0;
} catch(e) {
this.handleError('Parsing: ' + reply);
return 1;
}
},
requestToken : function () {
this.clearLabels();
EncodeHelper.token = "";
EncodeHelper.tokenSecret = "";
RequestTokenDS.execute({});
},
Code: Select all
authorizeToken : function (responseText) {
//alert('responseText en authorizeToken: ' + responseText);
if (this.parseReply(responseText) != 0){
return;
}
try {
var authorizeUrl = '[url=https://api.twitter.com/oauth/authorize?oauth_token]https://api.twitter.com/oauth/authori...[/url]=' + EncodeHelper.token;
if (window.plugins){ //mobileDevice
window.plugins.childBrowser.showWebPage(authorizeUrl);
} else { // desktop
window.open(authorizeUrl);
}
} catch (err) {
this.handleError('Unexpected service error.');
}
},
Code: Select all
beforeSendTwit : function () {
this.clearLabels();
var messText = $('[name=sendInput]').val();
if (messText == ""){
this.handleError('Enter the message to send.');
return;
}
if (this.accessTokenExist){
this.sendTwit();
} else { // get accessToken to this.token
var pinCode = $('[name=PINInput]').val();
if (pinCode == ""){
this.handleError('Enter PIN code.');
return;
}
//alert('pinCode: ' +pinCode);
AccessTokenDS.execute({}); // will call setAccessToken and after that will call sendTwit
}
},
Code: Select all
setAccessToken : function (responseText) {
//alert('responseText en setAccessToken: ' + responseText);
if (this.parseReply(responseText) != 0){
return;
}
this.accessTokenExist = true;
$('[name=pinGrid]').hide();
this.sendTwit();
},
Code: Select all
sendTwit : function () {
SendDS.execute({});
},
parseError : function (jqXHR) {
var text;
if (jqXHR.responseText){
try {
var obj = jQuery.parseJSON(jqXHR.responseText);
text = obj.errors[0].message;
} catch(e) {
text = jqXHR.statusText;
}
} else {
text = jqXHR.statusText;
}
this.handleError(text);
}
}
#47;error>/)[1]);
return 1;
}
var array = reply.split('&');
var tmp = array[0].split('=',2);
EncodeHelper.token = unescape(tmp[1]);
tmp = array[1].split('=',2);
EncodeHelper.tokenSecret = unescape(tmp[1]);
return 0;
} catch(e) {
this.handleError('Parsing: ' + reply);
return 1;
}
},
Code: Select all
requestToken : function () {
this.clearLabels();
EncodeHelper.token = ""
EncodeHelper.tokenSecret = ""
RequestTokenDS.execute({});
},
Code: Select all
authorizeToken : function (responseText) {
//alert('responseText en authorizeToken: ' + responseText);
if (this.parseReply(responseText) != 0){
return;
}
try {
var authorizeUrl = 'https://api.twitter.com/oauth/authorize?oauth_token=' + EncodeHelper.token;
if (window.plugins){ //mobileDevice
window.plugins.childBrowser.showWebPage(authorizeUrl);
} else { // desktop
window.open(authorizeUrl);
}
} catch (err) {
this.handleError('Unexpected service error.');
}
},
Code: Select all
beforeSendTwit : function () {
this.clearLabels();
var messText = $('[name=sendInput]').val();
if (messText == ""){
this.handleError('Enter the message to send.');
return;
}
if (this.accessTokenExist){
this.sendTwit();
} else { // get accessToken to this.token
var pinCode = $('[name=PINInput]').val();
if (pinCode == ""){
this.handleError('Enter PIN code.');
return;
}
//alert('pinCode: ' +pinCode);
AccessTokenDS.execute({}); // will call setAccessToken and after that will call sendTwit
}
},
Code: Select all
setAccessToken : function (responseText) {
//alert('responseText en setAccessToken: ' + responseText);
if (this.parseReply(responseText) != 0){
return;
}
this.accessTokenExist = true;
$('[name=pinGrid]').hide();
this.sendTwit();
},
Code: Select all
sendTwit : function () {
SendDS.execute({});
},
parseError : function (jqXHR) {
var text;
if (jqXHR.responseText){
try {
var obj = jQuery.parseJSON(jqXHR.responseText);
text = obj.errors[0].message;
} catch(e) {
text = jqXHR.statusText;
}
} else {
text = jqXHR.statusText;
}
this.handleError(text);
}
}
/code
2) codeSHA1Lib = {
str2binb : function (str){
var bin = Array();
for(var i = 0; i < str.length * 8; i += 8){
bin[i>>5] |= (str.charCodeAt(i / 8) & 255) << (24 - i%32);
}
return bin;
},
Code: Select all
sha1_ft : function (t, b, c, d){
if(t < 20) return (b & c) | ((~b) & d);
if(t < 40) return b ^ c ^ d;
if(t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
},
Code: Select all
sha1_kt : function (t){
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
},
Code: Select all
safe_add : function (x, y){
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
},
Code: Select all
core_sha1 : function(x, len){
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for(var i = 0; i < x.length; i += 16){
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for(var j = 0; j < 80; j++){
if (j < 16){
w[j] = x[i + j];
} else {
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
}
var t = this.safe_add(this.safe_add(rol(a, 5), this.sha1_ft(j, b, c, d)),
this.safe_add(this.safe_add(e, w[j]), this.sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
},
Code: Select all
core_hmac_sha1 : function (key, data){
var bkey = this.str2binb(key);
if(bkey.length > 16) bkey = core_sha1(bkey, key.length * 8);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++){
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_sha1(ipad.concat(this.str2binb(data)), 512 + data.length * 8);
return core_sha1(opad.concat(hash), 512 + 160);
},
Code: Select all
binb2b64 : function (binarray) {
var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var str = '';
for(var i = 0; i < binarray.length * 4; i += 3){
var triplet = (((binarray[i >> 2] >> 8*(3 - i%4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8*(3 - (i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8*(3 - (i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++){
if(i*8 + j*6 > binarray.length * 32){
str += '=';
} else {
str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
}
return str;
}
}
EncodeHelper = {
Code: Select all
token : "",
tokenSecret : "",
generateNonce : function () {
var nonce = '';
for (var i = 0; i < 8; i++) {
var character = Math.floor(Math.random() * 61);
nonce += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.substring(character, character + 1);
}
return nonce;
},
encodeSymbols : function (string){
return encodeURIComponent(string).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
},
Code: Select all
calcHash : function (key, data) {
var hash = SHA1Lib.core_hmac_sha1(key, data);
return SHA1Lib.binb2b64(hash);
},
getHeader : function (apiUrl, params) {
if (typeof params == 'undefined') {
var params = {};
}
Code: Select all
//signParams to generate signature
var signParams = {
oauth_consumer_key: TwitterSearchSettings.consumerKey,
oauth_version: '1.0',
oauth_timestamp: Math.round(new Date().getTime() / 1000),
oauth_nonce: this.generateNonce(),
oauth_signature_method: 'HMAC-SHA1'
};
if (this.token != ""){
signParams['oauth_token'] = this.token;
}
// oauthParams contains the parameters for authorization string
var oauthParams = {};
for (var i in signParams) {
oauthParams[i] = signParams[i];
}
// add params to generate signature in signParams
for (var key in params) {
var value = params[key];
signParams[key] = this.encodeSymbols(value);
}
var keys = [];
for (var key in signParams) {
keys.push(key);
}
keys.sort();
var signBaseString = '';
for (var i=0; i<keys.length; i++) {
var key = keys[i];
var value = signParams[key];
signBaseString += key + '=' + value + '&';
}
signBaseString = signBaseString.substring(0, signBaseString.length - 1);
signBaseString = 'POST&' + this.encodeSymbols(apiUrl) + '&' + this.encodeSymbols(signBaseString);
var signingKey = TwitterSearchSettings.consumerKeySecret + '&';
if (this.tokenSecret != ""){
signingKey += this.tokenSecret;
}
var signature = this.calcHash(signingKey, signBaseString);
oauthParams['oauth_signature'] = signature;
// creating authorization string
var authorization = 'OAuth ';
for (var key in oauthParams) {
var value = oauthParams[key];
authorization += key + '="' + this.encodeSymbols(value) + '", ';
}
return authorization.substring(0, authorization.length - 2);
}
}/code
3) code//correct replacement instead of standard from jQuery.
jQuery.param = function( a, traditional ) {
var prefix,
s = [],
add = function( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
s[ s.length ] = EncodeHelper.encodeSymbols( key ) + "=" + EncodeHelper.encodeSymbols( value );
};
// Set traditional to true for jQuery <= 1.3.2 behavior.
if ( traditional === undefined ) {
traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
}
// If an array was passed in, assume that it is an array of form elements.
if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
});
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for ( prefix in a ) {
buildParams( prefix, a[ prefix ], traditional, add );
}
}
// Return the resulting serialization
return s.join( "&" ).replace( /%20/g, "+" );
};
function buildParams( prefix, obj, traditional, add ) {
var name;
if ( jQuery.isArray( obj ) ) {
// Serialize array item.
jQuery.each( obj, function( i, v ) {
if ( traditional || rbracket.test( prefix ) ) {
// Treat each array item as a scalar.
add( prefix, v );
Code: Select all
} else {
// If array item is non-scalar (array or object), encode its
// numeric index to resolve deserialization ambiguity issues.
// Note that rack (as of 1.0.0) can't currently deserialize
// nested arrays properly, and attempting to do so may cause
// a server error. Possible fixes are to modify rack's
// deserialization algorithm or to provide an option or flag
// to force array serialization to be shallow.
buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add );
}
});
} else if ( !traditional && jQuery.type( obj ) === "object" ) {
// Serialize object item.
for ( name in obj ) {
buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
}
} else {
// Serialize scalar item.
add( prefix, obj );
}
}
/code