Skip to content

Commit 8aa20e9

Browse files
committed
Updated authenticate.mjs to support credentials in config locations
1 parent 0e1fd67 commit 8aa20e9

File tree

4 files changed

+452
-220
lines changed

4 files changed

+452
-220
lines changed

README.md

+54-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ If `npm install` causes a heap error during rollup, try `node --max-old-space-si
3030
Google Tasks API an authenticated OAuth2 client:
3131

3232
1. Go [here](https://developers.google.com/tasks/quickstart/nodejs), and click "Enable the Google Tasks API" button. Follow the steps to download the credentials.json file.
33-
2. Move credentials.json to your MMM-GoogleTasks directory (MagicMirror/modules/MMM-GoogleTasks/)
33+
2. Place the `credentials.json` file in a local directory. It can live alongside your MagicMirror configuration file (`config.js`).
3434
3. [Enable Google Tasks API](https://console.cloud.google.com/apis/library/tasks.googleapis.com). Select the same project as in step 1.
35-
4. Run authenticate.js:
36-
`node authenticate.mjs`
37-
5. Follow the instructions and it should print your lists. Copy the ID of the list you want to the config listID
35+
4. Run authenticate.mjs, specifying the location of your credential and token file:
36+
`node authenticate.mjs -c /path/to/credentials.json -t /path/to/token.json`
37+
5. Follow the instructions to generate tokens. The account name value used when storing tokens in the token.json file. This name is referenced in the `accounts` section of the [configuration](#configuration-options).
38+
6. Repeat steps 4 and 5 for each account you want to authenticate. Note that every account you want to add must be added to the [Test users](https://console.cloud.google.com/apis/credentials/consent) section for your application.
3839

3940
## Configuration
4041

@@ -51,7 +52,11 @@ var config = {
5152
header: "Google Tasks",
5253
position: "top_left",
5354
config: {
54-
listID: "",
55+
credentialPath: "/path/to/credentials.json",
56+
tokenPath: "/path/to/token.json",
57+
accounts: [
58+
59+
]
5560
...
5661
// See below for Configuration Options
5762
}
@@ -65,7 +70,10 @@ var config = {
6570

6671
| Option | Required | Details | Default |
6772
| ------------------ | -------- | ---------------------------------------------------------------------------------------- | ------------------------ |
68-
| `listID` | Yes | List ID printed from authenticate.mjs (see installation) | |
73+
| `credentialPath` | Yes | Path to the `credentials.json` file | |
74+
| `tokenPath` | Yes | Path to the `token.json` file | |
75+
| `accounts` | Yes | See [Accounts Configuration](#accounts-configuration) | |
76+
| `plannedTasks` | Yes | See [Planned Task Configuration](#planned-tasks-configuration) | `{ enabled: false }` |
6977
| `maxResults` | No | Max number of list items to retrieve. | 10 |
7078
| `showCompleted` | No | Show completed task items | `false` |
7179
| `maxWidth` | No | Width of the table | `450px` |
@@ -74,7 +82,44 @@ var config = {
7482
| `animationSpeed` | No | Speed of the update animation. (Milliseconds) | `2000` (2 seconds) |
7583
| `initialLoadDelay` | No | Delay before first load (Milliseconds) | `1500` (1.5 seconds) |
7684
| `ordering` | No | The method to order results. `myorder`, `due`, `title`, `updated` | `myorder` |
77-
| `plannedTasks` | Yes | See [Planned Task Configuration](#planned-tasks-configuration) | `{ enabled: false }` |
85+
86+
87+
### Accounts Configuration
88+
89+
The `accounts` section allows you to configure different accounts from which to pull Google tasks. Each account object can have the following properties:
90+
91+
| Option | Required | Details | Default |
92+
| --------------- | -------- | -------------------------------------------------------------------------------------------------------- | ------- |
93+
| `name` | Yes | The name of the account. There should be a token in your token.json file with the same name. | |
94+
| `includedLists` | Yes | A list of strings or Regex expressions used to determine whether the Google task list should be included | |
95+
96+
#### Acounts Configuration Example
97+
98+
```js
99+
{
100+
module: "MMM-GoogleTasks",
101+
header: "Google Tasks",
102+
position: "top_right",
103+
config: {
104+
credentialPath: "/path/to/credentials.json",
105+
tokenPath: "/path/to/token.json",
106+
ordering: "due",
107+
accounts: [
108+
{
109+
name: "work",
110+
includedLists: ["Inbox"]
111+
},
112+
{
113+
name: "personal",
114+
includedLists: ["Reminders"]
115+
}
116+
],
117+
plannedTasks: {
118+
enable: false
119+
}
120+
}
121+
}
122+
```
78123

79124
### Planned Tasks Configuration
80125

@@ -96,7 +141,8 @@ In other words, if you just enable the Planned Tasks configuration, the default
96141
header: "Google Tasks",
97142
position: "top_right",
98143
config: {
99-
listID: "MTc0NDU5MjE5OTkwMzc4MTE4NjU6NDcyMjk5MTIwOjA",
144+
credentialPath: "/path/to/credentials.json",
145+
tokenPath: "/path/to/token.json",
100146
ordering: "due",
101147
plannedTasks: {
102148
enable: true,

authenticate.mjs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
import { program } from 'commander';
2+
13
import { google } from 'googleapis';
24
import readline from 'readline';
35
import fs from 'fs';
46

57
// If modifying these scopes, delete token.json.
68
const SCOPES = ['https://www.googleapis.com/auth/tasks.readonly'];
7-
const TOKEN_PATH = 'token.json';
9+
10+
program
11+
.option('-c, --credential-file <credentialFile>', 'path to credential file', './credentials.json')
12+
.option('-t, --token-file <tokenFile>', 'path to token file', './token.json')
13+
14+
program.parse(process.argv);
15+
const options = program.opts();
816

917
// Load client secrets from a local file.
10-
fs.readFile('credentials.json', (err, content) => {
18+
fs.readFile(options.credentialFile, (err, content) => {
1119
if (err) return console.log('Error loading client secret file:', err);
1220

1321
let currentTokens = [];
14-
if (fs.existsSync(TOKEN_PATH)) {
15-
const token = fs.readFileSync(TOKEN_PATH, "utf-8");
22+
if (fs.existsSync(options.tokenFile)) {
23+
const token = fs.readFileSync(options.tokenFile, "utf-8");
1624

1725
currentTokens = JSON.parse(token);
1826
currentTokens.forEach(element => {

0 commit comments

Comments
 (0)