public
Last active

jsOAuth boilerplate for PIN based authentication in javascript

  • Download Gist
boilerplate.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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
pin_auth.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
<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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.