-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from yasuharu519/develop
[Prod] Release 2021-04-03 16:44:48
- Loading branch information
Showing
4 changed files
with
471 additions
and
18 deletions.
There are no files selected for viewing
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
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); | ||
}); |
Oops, something went wrong.