You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+52-1Lines changed: 52 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ Note that `commonTools.letsEncrypt` is not available anymore as the next control
81
81
82
82
## I18n
83
83
84
-
Developer can use internationalisation in backend.
84
+
Developers can use internationalisation in backend.
85
85
86
86
For that call
87
87
@@ -100,6 +100,10 @@ and then in code
100
100
101
101
```javascript
102
102
console.log(I18n.translate('text to translate %s', 'argument1'));
103
+
104
+
// Or you can use short form
105
+
console.log(I18n.t('text to translate %s', 'argument1'));
106
+
103
107
// or to get the ioBroker.Translated object
104
108
console.log(JSON.stringify(I18n.getTranslatedObject('text to translate %s and %s', 'argument1', 'argument2')));
105
109
```
@@ -148,6 +152,49 @@ ioBroker has the ability to include files written by adapters in its backups. To
148
152
149
153
This path is relative to the path returned by `getAbsoluteDefaultDataDir()`. The placeholder `%INSTANCE%` is automatically replaced by the instance number of each adapter, for example `"dataFolder": "my-adapter.%INSTANCE%"`.
150
154
155
+
## OAuth2 token refresher
156
+
To keep the OAuth2 access token up to date, you can use the `TokenRefresher` class and do not forget to include `axios` in your adapter dependencies (in `package.json`).
157
+
158
+
```Typescript
159
+
import { TokenRefresher } from '@iobroker/adapter-core';
160
+
161
+
export class YourAdapter extends Adapter {
162
+
private tokenWorker?: TokenRefresher;
163
+
164
+
// It is important to call this method in the `onReady` method of your adapter and not in the constructor.
165
+
async onReady(): Promise<void> {
166
+
// Not in constructor, but in onReady
167
+
this.tokenWorker = new TokenRefresher(this, 'yourService');
168
+
// Your other initialization code...
169
+
170
+
// Then later in code, you can get the access token like this:
// It detects `adapterName.X.oauth2Tokens` or similar
181
+
this.tokenWorker?.onStateChange(id, state);
182
+
// Your other state change code...
183
+
}
184
+
185
+
onUnload(callback: () => void): void {
186
+
this.tokenWorker?.destroy();
187
+
// Your other unload code...
188
+
callback();
189
+
}
190
+
}
191
+
```
192
+
193
+
Important to use OAuth2 infrastructure in your adapter [contact](mailto:info@iobroker.net) the ioBroker team so we can implement a cloud code for https://oauth2.iobroker.in/YOUR_SERVICE_NAME.
194
+
This will allow you to use the OAuth2 infrastructure in your adapter without having to implement it yourself.
195
+
196
+
Here is a description of how to implement OAuth2 in your adapter: https://github.com/ioBroker/ioBroker.admin/blob/master/packages/jsonConfig/OAUTH2.md
197
+
151
198
## Tips while working on this module
152
199
153
200
- `npm run build` creates a clean rebuild of the module. This is done automatically before every build;
@@ -164,6 +211,10 @@ If you find errors in the definitions, e.g., function calls that should be allow
164
211
Placeholder for the next version (at the beginning of the line):
165
212
### **WORKINPROGRESS**
166
213
-->
214
+
### **WORKINPROGRESS**
215
+
216
+
- (@GermanBluefox) Added the token refresher class
/** Interval in seconds, when access token will expire */
8
+
expires_in: number;
9
+
/** The date and time when the access token expires, in ISO format */
10
+
access_token_expires_on: string;
11
+
/** Extended expiration time in seconds, when the access token will expire */
12
+
ext_expires_in: number;
13
+
/** Type */
14
+
token_type: 'Bearer';
15
+
/** Scopes */
16
+
scope: string;
17
+
/** The refresh token used to obtain a new access token */
18
+
refresh_token: string;
19
+
}
20
+
/**
21
+
* TokenRefresher class manages OAuth2 access tokens for an ioBroker adapter.
22
+
*/
23
+
exportdeclareclassTokenRefresher{
24
+
privatereadonlyadapter;
25
+
privatereadonlystateName;
26
+
privaterefreshTokenTimeout;
27
+
privateaccessToken;
28
+
privatereadonlyurl;
29
+
privatereadonlyreadyPromise;
30
+
privatereadonlyname;
31
+
/** Threshold in milliseconds before the access token expires to trigger a refresh */
32
+
staticTOKEN_REFRESH_THRESHOLD_MS: number;
33
+
/**
34
+
* Creates an instance of TokenRefresher.
35
+
*
36
+
* @param adapter Instance of ioBroker adapter
37
+
* @param serviceName Name of the service for which the tokens are managed, e.g., 'spotify', 'dropbox', etc.
38
+
* @param stateName Optional name of the state where tokens are stored. Defaults to 'oauth2Tokens' and that will store tokens in `ADAPTER.X.oauth2Tokens`.
0 commit comments