Created
August 3, 2012 19:33
-
-
Save jzazove/3250743 to your computer and use it in GitHub Desktop.
Tip or Skip OAuth nodejs Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Connecting to the Tip or Skip API with Node.js. | |
In your package.json, require the oauth module and install it with `npm update`: | |
```javascript | |
// ... | |
dependencies: { | |
"oauth": "x.x.x" | |
}, | |
// ... | |
``` | |
In your application, require the oauth module: | |
```javascript | |
var OAuth = require("oauth").OAuth; | |
``` | |
Set up the OAuth object: | |
```javascript | |
var my_oauth_callback = "http://my.domain/handle/authorize"; | |
var authorize_url = "https://tips.by/oauth/authorize?oauth_token="; | |
var access_token_url = "https://tips.by/oauth/access_token"; | |
var request_token_url = "https://tips.by/oauth/request_token?oauth_callback=" + my_oauth_callback; | |
var tos_auth = new OAuth(request_token_url, access_token_url, api_key, api_secret, "1.0", null, "HMAC-SHA1"); | |
``` | |
Create a route to handle authorization (examples use Express): | |
```javascript | |
app.get("/handle/authorize", function(request, response, next) { | |
if (!request.query.oauth_verifier) { | |
return response.redirect("/handle/denied_authorization", 302); | |
}; | |
oauth.getOAuthAccessToken(request.session.request_token, request.session.request_token_secret, request.query.oauth_verifier, function(error, access_token, access_token_secret, additional_data) { | |
request.session.tos_user_id = additional_data.user_id; | |
request.session.tos_user_name = additional_data.user_name; | |
request.session.access_token = access_token; | |
request.session.access_token_secret = access_token_secret; | |
return response.redirect("/action/that/required/authorization", 302); | |
}); | |
}); | |
``` | |
Get a request token: | |
```javascript | |
app.get("/some/route/that/requires/access", function(request, response, next) { | |
if (!request.session.access_token) { | |
oauth.getOAuthRequestToken(function(error, request_token, request_secret) { | |
if (error) { | |
return next(error); | |
}; | |
request.session.request_token = request_token; | |
request.session.request_token_secret = request_secret; | |
return response.redirect(authorize_url + request_token, 302); | |
}); | |
} | |
else { | |
// ... | |
} | |
}); | |
``` | |
Post to a protected resource: | |
```javascript | |
// tip a fake product yay | |
oauth.post("https://tips.by/api/1.0/product/4e84efceaf17d7ab1200000c/tip?access_token=" + request.session.access_token, request.session.access_token, request.session.access_token_secret, "{}", "application/json", function(error, response) { | |
if (error) { | |
//... | |
}; | |
// success | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment