forked from hexplus/orcid-api-walkthrough
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (49 loc) · 1.7 KB
/
server.js
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
55
56
57
58
59
60
61
62
63
64
65
66
var express = require('express');
var port = process.env.EXPRESSPORT != undefined ? process.env.EXPRESSPORT: '8000'
// Set the client credentials and the OAuth2 server
var credentials = {
clientID: 'APP-LYNIW43A4OO9DH72',
clientSecret: '43091822-851a-4b1e-8ee6-20d55ad06a40',
site: 'https://sandbox.orcid.org',
tokenPath:'https://api.sandbox.orcid.org/oauth/token'
};
// Initialize the OAuth2 Library
var oauth2 = require('simple-oauth2')(credentials);
// Authorization oauth2 URI
var authorization_uri = oauth2.authCode.authorizeURL({
redirect_uri: 'http://localhost:8000/callback',
scope: '/activities/update /authenticate',
state: 'nope'
});
var app = express();
app.set('view engine', 'ejs');
//app.use(require('connect-livereload')({port: 35729}));
app.use(express.static(__dirname + '/public'));
// index page
app.get('/', function(req, res) {
res.render('pages/index', {
min: process.env.MINIMIZE == 'true' ? '.min':''
});
});
// Redirect example using Express (see http://expressjs.com/api.html#res.redirect)
app.get('/get-auth', function(req, res) {
// Prepare the context
res.redirect(authorization_uri);
});
// Get the access token object (the authorization code is given from the previous step).
app.get('/callback', function(req, res) {
var token;
var code = req.query.code;
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://localhost:8000/callback'
}, function(error, result){
token = oauth2.accessToken.create(result);
// TODO: save token here, to be read by show-work later
res.redirect('http://localhost:8000/show-work');
});
});
// TODO: make show-work page
app.listen(port, function (){
console.log('server started on ' + port);
});