Skip to content

@bytespider /boilerplate.coffee
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.