Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bring your own credentials (issue #64) #74

Merged
merged 7 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ For command line use, install md2gslides globally:
npm install -g md2gslides
```

Then get your OAuth client ID credentials:

* Create (or reuse) a developer project at <https://console.developers.google.com>
* Enable Google Slides API at [API library page](https://console.developers.google.com/apis/library)
* Go to [Credentials page](https://console.developers.google.com/apis/credentials) and click "+ Create credentials" at the top
* Select "OAuth client ID" authorization credentials
* Choose type "Computer Application" and give it some name.
* Download client credentials file.
* Copy it to `client_id.json` (name has to match) and save to `~/.md2googleslides`.

After installing, import your slides by running:

```sh
md2gslides slides.md
```

The first time the command is run you will be prompted for authorization. Credentials
will be stored locally in a file named `~/.credentials/md2gslides.json`.
The first time the command is run you will be prompted for authorization. OAuth token
credentials will be stored locally in a file named `~/.md2googleslides/credentials.json`.

## Supported markdown rules

Expand Down
19 changes: 17 additions & 2 deletions bin/md2gslides.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const SCOPES = ['https://www.googleapis.com/auth/presentations', 'https://www.go

const USER_HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
const STORED_CREDENTIALS_PATH = path.join(USER_HOME, '.md2googleslides', 'credentials.json');
const STORED_CLIENT_ID_PATH = path.join(USER_HOME, '.md2googleslides', 'client_id.json');

var parser = new ArgumentParser({
version: '1.0.0',
Expand Down Expand Up @@ -124,9 +125,23 @@ function authorizeUser() {
// application/utility such as this. Of course, in such cases the "secret" is
// actually publicly known; security depends entirely on the secrecy of refresh
// tokens, which effectively become bearer tokens.

wescpy marked this conversation as resolved.
Show resolved Hide resolved
// Load and parse client ID and secret from client_id.json file. (Create
// OAuth client ID from Credentials tab at console.developers.google.com
// and download the credentials as client_id.json to ~/.md2googleslides
var data; // needs to be scoped outside of try-catch
try {
data = fs.readFileSync(STORED_CLIENT_ID_PATH);
} catch(err) {
return console.log('Error loading client secret file:', err);
}
if(data === undefined) return console.log('Error loading client secret data:', err);
const creds = JSON.parse(data).installed;
Comment on lines +132 to +139
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that might help maintainers to get this merged is focusing on details, let's have this code more resilient. I think we have couple of things we could improve: hugs

  • JSON parsing can error out easily, we should catch those! (:
  • data == undefined is not very helpful. Also at this point we don't have err variable anymore.

Also I am not the super experienced with typescript (Go dev here), is there more idiomatic way to check errors than exceptions? 🤔

What about something like this?

Suggested change
var data; // needs to be scoped outside of try-catch
try {
data = fs.readFileSync(STORED_CLIENT_ID_PATH);
} catch(err) {
return console.log('Error loading client secret file:', err);
}
if(data === undefined) return console.log('Error loading client secret data:', err);
const creds = JSON.parse(data).installed;
var creds;
if (!fs.existsSync(STORED_CLIENT_ID_PATH)) {
return console.log('Client ID + Secret does not exists in path:', STORED_CLIENT_ID_PATH);
}
var data;
try {
data = fs.readFileSync(STORED_CLIENT_ID_PATH);
creds = JSON.parse(data.toString());
} catch (err) {
return console.log('Error loading client secret file:', err);
}

Tried here https://github.com/gsuitedevs/md2googleslides/compare/master...bwplotka:auth?expand=1 and works smoothly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Looks good. The only thing I would change would be to shorten your warning string so line 134 is: return console.log('Credentials file missing from:', STORED_CLIENT_ID_PATH);, and a similar tweak can be made to line 142.


// Authorize user and get (& store) a valid access token.
const options = {
clientId: '52512509792-pc54t7beete33ifbhk00q3cpcpkmfi7c.apps.googleusercontent.com',
clientSecret: '8g6up8tcVXgF7IO71mCN8Afk',
clientId: creds.client_id,
clientSecret: creds.client_secret,
filePath: STORED_CREDENTIALS_PATH,
prompt: prompt,
};
Expand Down