Skip to content

Commit

Permalink
generateUrl opens browser automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Cámara committed Sep 21, 2017
1 parent e02ac4b commit 0fcdde9
Show file tree
Hide file tree
Showing 4 changed files with 555 additions and 87 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ npm install -g gcal

## Authentication

Authorization and authentication is done with OAuth 2.0.
Authorization and authentication is done with OAuth 2.0.

Ok, this will take only 2 minutes:

Expand All @@ -81,6 +81,8 @@ $ gcal generateUrl

The page will prompt you to authorize access, follow the instructions.

If you add the `-o` option to the command, your default browser will be opened automatically with the consent page.

#### 3) Get the token!

With the code we got through the authorization page, we can obtain a token and store it in our machine.
Expand All @@ -89,7 +91,7 @@ With the code we got through the authorization page, we can obtain a token and s
$ gcal storeToken <code>
```

([By default](#overwriting-default-config) the token is stored in your home folder under the name `calendar_api_token.json`).
([By default](#overwriting-default-config) the token is stored in your home folder under the name `calendar_api_token.json`).

NOTE: The token will expiry after one hour, but a `refresh_token` is included as well, allowing the app to refresh automatically the token each time it's used.

Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const chalk = require('chalk');
const moment = require('moment');
const conf = require('./conf');
const help = require('./help');
const opn = require('opn');

/**
* Get absolute path
Expand Down Expand Up @@ -64,13 +65,16 @@ const getClient = async () => {
* Generate consent page URL
* @returns {Promise}
*/
const generateUrl = async () => {
const generateUrl = async (openInBrowser) => {
const oauth2Client = getOauth2Client();
const authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: conf.SCOPES
});
console.log(authUrl);
if (openInBrowser) {
opn(authUrl, { wait: 0 });
}
};

/**
Expand Down Expand Up @@ -185,7 +189,7 @@ const insert = async (naturalInfo, options) => {
date: moment(date).format('YYYY-MM-DD')
};
event.end = {
date: duration ?
date: duration ?
moment(date).add(duration.slice(0, -1), duration.slice(-1)).format('YYYY-MM-DD') :
moment(date).add(1, 'd').format('YYYY-MM-DD')
};
Expand All @@ -195,7 +199,7 @@ const insert = async (naturalInfo, options) => {
dateTime: moment(dateTime).format()
};
event.end = {
dateTime: duration ?
dateTime: duration ?
moment(dateTime).add(duration.slice(0, -1), duration.slice(-1)).format() :
moment(dateTime).add(conf.EVENT_DURATION, 'm').format()
};
Expand Down Expand Up @@ -249,7 +253,7 @@ console.log(argv);
}
switch (command) {
case 'generateUrl': {
await generateUrl();
await generateUrl(argv.o);
break;
}
case 'storeToken': {
Expand Down
Loading

0 comments on commit 0fcdde9

Please sign in to comment.