Skip to content

Commit

Permalink
Merge pull request #36 from yasuharu519/develop
Browse files Browse the repository at this point in the history
[Prod] Release 2021-04-03 16:44:48
  • Loading branch information
yasuharu519 authored Apr 3, 2021
2 parents 6c80d0d + 46c4c90 commit 15b6669
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 18 deletions.
43 changes: 43 additions & 0 deletions examples/express-authentication/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
let express = require('express');
let WithingsClient = require('withings-ts').default
let app = express();

// First you need register application on https://account.withings.com/partner/add_oauth2
// After registration, you can get client id and client secret, please put those here.
// While registration, you can set callback url for authentication process, please put same one here.
let client = new WithingsClient(
"<client id>",
"<client secret>",
"<callback url>"
)

// 1. First you need redirect user to get authentication code
app.get('/', function (req, res) {
console.log(client)
res.redirect(client.generateGetAuthenticationCodeUrl("<state>", ["<scopes>"]))
});

// 2. On callback url, you can get authentication code by query parameter.
// With that authentication code, you can get access token and refresh token.
// With using access token, you can call api whatever you want.
app.get('/callback', function (req, res) {
console.log(req.query);

// authentication code can be got through query parameter
let code = req.query.code;

// get access token by using authentication code
client.getAccessToken(code).then(function (accessTokenRes) {
// with access token, you can call api whatever you want limited by scope.
// access token only valid 3 hours by default.
// If you want extend expiration, you need to refresh access token by calling `getRefreshToken` and get new access token
res.json(accessTokenRes);
});
});

// start app server
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
Loading

0 comments on commit 15b6669

Please sign in to comment.