@@ -25,71 +25,96 @@ npm install netatmo-nodejs-api
2525
2626## Usage
2727
28- ### Basic example with refresh token and client credentials grant type
28+ You need to [ create an application] ( https://dev.netatmo.com/apps/createanapp#form )
29+
30+ ### ~~ Basic example with Client Credentials grant type~~
31+
32+ This method has been deprecated, see [ Natatmo documentation] ( https://dev.netatmo.com/apidocumentation/oauth#client-credential )
33+
34+ ### Basic example with Refresh Token grant type
35+
36+ You need to generate a token on [ Netatmo website] ( https://dev.netatmo.com/apps/ ) :
37+ - choose scopes
38+ - click ` generate token ` button and accept the condition
39+ - copy both ` Access Token ` and ` Refresh Token ` and use it in the following code
40+
2941``` js
3042const { NetatmoClient , SCOPE_BASIC_CAMERA } = require (' netatmo-nodejs-api' )
3143
32- // you need to set your own information
33- const clientId = ' 60...'
34- const clientSecret = ' abc...'
35- const username = ' user@domain'
36- const password = ' pass'
37- let refreshToken = ' '
38- let accessToken = ' '
39- let expiresInTimestamp = 0
40-
41- try {
42- // create client
43- const client = new NetatmoClient (clientId, clientSecret, SCOPE_BASIC_CAMERA , { timeout: 1000 })
44-
45- // authenticate
46- if (! client .checkAndSetAccesToken (accessToken, expiresInTimestamp)) {
47- if (refreshToken) {
48- // use previous refresh token
49- ({ accessToken, refreshToken, expiresInTimestamp } = await client .authenticateByRefreshToken (refreshToken))
50- } else {
51- // use user credentials
52- ({ accessToken, refreshToken, expiresInTimestamp } = await client .authenticateByClientCredentials (username, password))
44+ async function main () {
45+ // you need to set your own information
46+ const clientId = ' '
47+ const clientSecret = ' '
48+ let accessToken = ' '
49+ let refreshToken = ' '
50+ let expiresInTimestamp = 0
51+
52+ try {
53+ // create client
54+ const client = new NetatmoClient (clientId, clientSecret, SCOPE_BASIC_CAMERA , { timeout: 1000 })
55+
56+ // authenticate
57+ if (! client .checkAndSetAccesToken (accessToken, expiresInTimestamp)) {
58+ if (refreshToken) {
59+ // use previous refresh token
60+ ({ accessToken, refreshToken, expiresInTimestamp } = await client .authenticateByRefreshToken (refreshToken))
61+ // you should store accessToken, refreshToken, expiresInTimestamp for further request
62+ console .log (' update the code with following 3 lines:' )
63+ console .log (` let accessToken = '${ accessToken} '` )
64+ console .log (` let refreshToken = '${ refreshToken} '` )
65+ console .log (` let expiresInTimestamp = ${ expiresInTimestamp} ` )
66+ } else {
67+ throw new Error (' Refresh token is missing' )
68+ }
5369 }
54- // you should store accessToken, refreshToken, expiresInTimestamp for further request
55- }
5670
57- // get data
58- const homes = await client .getHomes ()
59- } catch (error) {
60- console .log (error)
71+ // get data
72+ const homes = await client .getHomes ()
73+ console .log (homes)
74+ } catch (error) {
75+ console .log (error)
76+ }
6177}
78+
79+ main ()
6280```
6381
64- ### Authenticate wrapper (try access token, refresh token or client credentials )
82+ ### Authenticate wrapper (try access token or refresh token )
6583
66- You can use the ` authenticate ` method which wrap 3 authentication methods.
84+ You can use the ` authenticate ` method which wrap 2 authentication methods.
6785
6886``` js
6987const { NetatmoClient , SCOPE_BASIC_CAMERA } = require (' netatmo-nodejs-api' )
7088
71- // you need to set your own information
72- const clientId = ' 60...'
73- const clientSecret = ' abc...'
74- const username = ' user@domain'
75- const password = ' pass'
76- let refreshToken = ' '
77- let accessToken = ' '
78- let expiresInTimestamp = 0
79-
80- try {
81- // create client
82- const client = new NetatmoClient (clientId, clientSecret, SCOPE_BASIC_CAMERA , { timeout: 1000 })
83-
84- // authenticate
85- ({ accessToken, refreshToken, expiresInTimestamp } = await client .authenticate (accessToken, refreshToken, expiresInTimestamp, username, password))
86- // you should store accessToken, refreshToken, expiresInTimestamp for further request
87-
88- // get data
89- const homes = await client .getHomes ()
90- } catch (error) {
91- console .log (error)
89+ async function main () {
90+ // you need to set your own information
91+ const clientId = ' '
92+ const clientSecret = ' '
93+ let refreshToken = ' '
94+ let accessToken = ' '
95+ let expiresInTimestamp = 0
96+
97+ try {
98+ // create client
99+ const client = new NetatmoClient (clientId, clientSecret, SCOPE_BASIC_CAMERA , { timeout: 1000 });
100+
101+ // authenticate
102+ ({ accessToken, refreshToken, expiresInTimestamp } = await client .authenticate (accessToken, refreshToken, expiresInTimestamp))
103+ // you should store accessToken, refreshToken, expiresInTimestamp for further request
104+ console .log (' update the code with following:' , refreshToken)
105+ console .log (` let accessToken = '${ accessToken} '` )
106+ console .log (` let refreshToken = '${ refreshToken} '` )
107+ console .log (` let expiresInTimestamp = ${ expiresInTimestamp} ` )
108+
109+ // get data
110+ const homes = await client .getHomes ()
111+ console .log (homes)
112+ } catch (error) {
113+ console .log (error)
114+ }
92115}
116+
117+ main ()
93118```
94119
95120## Versioning
0 commit comments