Skip to content

Instantly share code, notes, and snippets.

@kanreisa
Created October 9, 2010 09:17
Show Gist options
  • Save kanreisa/618047 to your computer and use it in GitHub Desktop.
Save kanreisa/618047 to your computer and use it in GitHub Desktop.
/*
- API Access -
*/
function makeOAuthSign(pUrl, pParameters)
{
pUrl = oauth.apiroot + pUrl;
// create Array
var accessor = {
consumerSecret : oauth.consumerSecret,
tokenSecret : oauth.tokenSecret
};
var message = {
action : pUrl,
method : oauth.method,
parameters : []
};
// push parameters for oauth
message.parameters.push(['oauth_consumer_key', oauth.consumerKey]);
message.parameters.push(['oauth_signature_method', oauth.signatureMethod]);
message.parameters.push(['oauth_token', oauth.token]);
// push parameters
if (pParameters && pParameters instanceof Array)
for (var p in pParameters)
if (pParameters[p] instanceof Array) message.parameters.push(pParameters[p]); //Arrayかどうか確認してundefinedを含めるのを避ける
// initialize OAuth
OAuth.setTimestampAndNonce(message);
OAuth.SignatureMethod.sign(message, accessor);
var parameterMap = OAuth.getParameterMap(message.parameters);
// convert for GET URL
var getParameter = '?';
for (var p in parameterMap){
//alert(p + ': ' + parameterMap[p]);
getParameter += p + '=' + parameterMap[p] + '&';
}
getParameter = getParameter.slice(0,-1); // 最後の&を除去
// Finish
var url = pUrl + getParameter;
// Return
//alert(url); // for debug
return url;
}
function accessAPI(pUrl, pParameters)
{
var url = makeOAuthSign(pUrl, pParameters);
var sc = document.createElement('script');
sc.setAttribute('type','text/javascript');
sc.setAttribute('src',url);
document.body.appendChild(sc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment