Skip to content

Instantly share code, notes, and snippets.

@bytespider
Created July 8, 2011 05:51
Show Gist options
  • Save bytespider/1071227 to your computer and use it in GitHub Desktop.
Save bytespider/1071227 to your computer and use it in GitHub Desktop.
jsOAuth boilerplate for PIN based authentication in javascript
openAuthoriseWindow = (url) ->
waitForPin = ->
if wnd.closed
pin = prompt("Please enter your PIN", "")
oauth.setVerifier pin
oauth.fetchAccessToken getSomeData, failureHandler
else
setTimeout waitForPin, 100
wnd = window.open(url, "authorise")
setTimeout waitForPin, 100
getSomeData = ->
oauth.get "https://api.example.com/oauth/something/?format=json", ((data) ->
console.log data.text
), failureHandler
failureHandler = (data) ->
console.error data
config =
consumerKey: "YOUR-K3Y"
consumerSecret: "YOUR-53CRET"
requestTokenUrl: "https://api.example.com/oauth/request_token"
authorizationUrl: "https://api.example.com/oauth/authorize"
accessTokenUrl: "https://api.example.com/oauth/access_token"
oauth = new OAuth(config)
oauth.fetchRequestToken openAuthoriseWindow, failureHandler
<html>
<head>
<title></title>
</head>
<body>
<script src="jsOAuth-1.3.min.js"></script>
<script>
var config = {
consumerKey: "YOUR-K3Y",
consumerSecret: "YOUR-53CRET",
requestTokenUrl: "https://api.example.com/oauth/request_token",
authorizationUrl: "https://api.example.com/oauth/authorize",
accessTokenUrl: "https://api.example.com/oauth/access_token"
};
var oauth = new OAuth(config);
oauth.fetchRequestToken(openAuthoriseWindow, failureHandler);
function openAuthoriseWindow(url)
{
var wnd = window.open(url, 'authorise');
setTimeout(waitForPin, 100);
function waitForPin()
{
if (wnd.closed)
{
var pin = prompt("Please enter your PIN", "");
oauth.setVerifier(pin);
oauth.fetchAccessToken(getSomeData, failureHandler);
}
else
{
setTimeout(waitForPin, 100);
}
}
}
function getSomeData()
{
oauth.get("https://api.example.com/oauth/something/?format=json", function (data) {
console.log(data.text);
}, failureHandler);
}
function failureHandler(data)
{
console.error(data);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment