Some days ago I want to connect to the new api provided by photobucket writting only javascript code.
I found that photobucket use OAuth but, from my POV, I need a quick and dirty -old style- function to get an unauthenticated call, to read all my public albums and tags.
After some digging into google, my outcome is "Nothing available" from the community (and no example in Javascript on photobucket samples).
I'm not a good javascript coder, butI'm proud of releasing this pieces of code, that could help others in accessing photobucket, and avoiding wasting time with encoding, timestamps and generating keys.
Assumption are:
usage of:
<script src="http://oauth.googlecode.com/svn/code/javascript/sha1.js">
declaration of:
var consumer_secret ="your_secret_by_Photobucket";
var consumer_key="your_consumer_key_by_Photobucket";
Here are the functions.function getAlbums( user, album ) {
var url = "http://api.photobucket.com/album/";
url = url + encodeURIComponent( user + "/" + album )
var json = callPhotobucket( url, "format=json" );
}
function getTag( media ) {
var url = "http://api.photobucket.com/media/";
url = url + encodeURIComponent( media ) + "/tag" ;
var json = callPhotobucket( url, "format=json" );
}
function getTimestamp() {
var timestamp = new Date().valueOf();
timestamp = timestamp / 1000;
timestamp = Math.ceil( timestamp );
return timestamp;
}
function ping() {
auth_url = callPhotobucket( "http://api.photobucket.com/ping", "format=json" );
}
function callPhotobucket( url, format ) {
try {
if( url == undefined ) {
url = "http://api.photobucket.com/ping";
}
if( format == undefined ) {
format = "format=json";
}
var timestamp = getTimestamp();
auth_nonce="nonce" + timestamp;
auth_method = "HMAC-SHA1";
auth_timestamp = "" + timestamp;
auth_version="1.0";
auth_consumer = "&oauth_consumer_key="+ encodeURIComponent( consumer_key );
nonce = "&oauth_nonce="+ encodeURIComponent( auth_nonce );
auth_sig_method = "&oauth_signature_method="+ encodeURIComponent( auth_method );
auth_timestamp = "&oauth_timestamp="+ encodeURIComponent( auth_timestamp );
version = "&oauth_version=" + encodeURIComponent( auth_version );
paramstring = format + auth_consumer + nonce + auth_sig_method + auth_timestamp + version;
method = "GET";
base = encodeURIComponent( method ) + "&" +
encodeURIComponent( url ) + "&" +
encodeURIComponent( paramstring );
sig_hash = getSignature( consumer_secret+"&", base );
auth_sign = "&oauth_signature=" + sig_hash;
auth_url = url + "?" + paramstring + "&" + auth_sign;
myalert( ""+ auth_url+"");
return auth_url;
}
catch (err) {
alert( "Error " + err );
}
}
function getSignature(key, baseString) {
b64pad = '=';
var signature = b64_hmac_sha1(key, baseString);
return signature;
}
Simply access to getAlbum( user, album ) to get albums of the user, or getTags( url_the_media ) to get tags of the media.
Enjoy.