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

Add node script for token #82

Merged
merged 23 commits into from
Mar 23, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"editor.formatOnSave": true
"editor.formatOnSave": true,
"eslint.enable": false
}
32 changes: 32 additions & 0 deletions avs-helper/avs-refreshToken-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const request = require("request");
const common_1 = require("./common");
console.log("Welcome to the Amazon auth helper!");
console.log("I will need four things from you:");
console.log("- Authorization code from previous step");
console.log("- Client ID");
console.log("- Client secret");
console.log("- Allowed return URL");
common_1.prompt("[Press any key to continue]").then(() => {
common_1.prompt("Authorization code (from previous step)?").then(authorizationCode => {
common_1.prompt("Client ID?").then(clientId => {
common_1.prompt("Client secret?").then(clientSecret => {
common_1.prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
request.post("https://api.amazon.com/auth/o2/token", {
form: {
grant_type: "authorization_code",
code: authorizationCode,
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectURI,
},
}, (error, response, body) => {
console.dir(JSON.parse(body), { colors: true });
process.exit();
});
});
});
});
});
});
37 changes: 37 additions & 0 deletions avs-helper/avs-token-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("./common");
const generateQuery = (params) => Object.keys(params)
.map((key) => key + "=" + params[key])
.join("&");
console.log("Welcome to the Amazon auth helper!");
console.log("I will need three things from you:");
console.log("- Client ID");
console.log("- Product ID");
console.log("- Allowed return URL");
common_1.prompt("[Press any key to continue]").then(() => {
common_1.prompt("Client ID?").then(clientId => {
common_1.prompt("Product Id?").then(productId => {
common_1.prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
const scopeData = {
"alexa:all": {
productID: productId,
productInstanceAttributes: {
deviceSerialNumber: 123,
},
},
};
const getParams = generateQuery({
client_id: clientId,
scope: "alexa:all",
scope_data: JSON.stringify(scopeData),
response_type: "code",
redirect_uri: redirectURI,
});
console.log("Great! Copy paste the link below to your browser to authorize your Alexa product");
console.log(`https://www.amazon.com/ap/oa?${getParams}`);
process.exit();
});
});
});
});
14 changes: 14 additions & 0 deletions avs-helper/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function prompt(question) {
return new Promise((resolve, reject) => {
const stdin = process.stdin;
const stdout = process.stdout;
stdin.resume();
stdout.write(question + " ");
stdin.once("data", data => {
resolve(data.toString().trim());
});
});
}
exports.prompt = prompt;
26 changes: 12 additions & 14 deletions docs/Installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ $ npm run electron-rebuild

## Configuring the Module

1. Go to https://magic-mirror-avs.github.io/Alexa-Web-Helper/ and generate a config for this alexa module. I have tried to make this step as simple as possible.
Next we need to authorize Awesome-Alexa with your Amazon account. We do this in two steps:

Sometimes, this does not work, because it would be missing a `Refresh Token`, please follow this guide to manually do it with these Three ways:
1. Get authorization token which will be used in next step
2. Here we use authorization token to get refresh token which will be passed to the Awesome-Alexa configuration

* https://github.com/dolanmiu/MMM-awesome-alexa/wiki/How-to-get-AVS-Token
### Configuration

* https://github.com/dolanmiu/MMM-awesome-alexa/wiki/How-to-get-AVS-Token-2

* > In the `help/bash-scripts` directory are two files: `auth_code.sh` and `auth_code2.sh`
> Run `auth_code.sh` first then the `auth_code2.sh`.
> **MAKE SURE YOU ENTER YOUR DATA IN THE FILES BEFORE RUNNING!**

The `config` should look like:
1. Run `avs-token-helper.js` with `npm run avs-token-helper`
2. Follow the on screen instructions and give the script info it needs.
3. After you got the authorization token run the refresh token helper with `npm run avs-refreshToken-helper`
4. Follow on screen instructions
5. Get `refresh_token` from the succesful response
6. Fill Awesome-Alexa module config with your information to your `config/config.js` file.

```json
{
Expand All @@ -81,11 +81,9 @@ $ npm run electron-rebuild
}
```

2. Then, add the config from the website above and put it into the `modules` section of your `config/config.js` file.

3. To configure the wake word, change the `wakeWord` property in the `config`:
7. To configure the wake word, change the `wakeWord` property in the `config`:
Alexa: `wakeWord: "Alexa"`
Smart Mirror: `wakeWord: "Smart Mirror"`
Snowboy: `wakeWord: "Snowboy"`

4. To turn on visualization, set `isSpeechVisualizationEnabled` to `true`.
8. To turn on visualization, set `isSpeechVisualizationEnabled` to `true`.
9 changes: 0 additions & 9 deletions help/bash-scripts/README.md

This file was deleted.

15 changes: 0 additions & 15 deletions help/bash-scripts/auth_code.sh

This file was deleted.

9 changes: 0 additions & 9 deletions help/bash-scripts/auth_code2.sh

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"node-pre-gyp": "node-pre-gyp clean configure build",
"test-rec": "bash test-rec.sh",
"spell": "cspell src/**/*.ts",
"style": "prettier -l src/**/*.ts"
"style": "prettier -l src/**/*.ts",
"avs-token-helper": "node avs-helper/avs-token-helper.js",
"avs-refreshToken-helper": "node avs-helper/avs-refreshToken-helper.js",
"build.avs-helper": "tsc --p tsconfig.avs-helper.json"
},
"author": "Dolan Miu",
"license": "MIT",
Expand Down
36 changes: 36 additions & 0 deletions src/avs-token-helper/avs-refreshToken-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as request from "request";
import { prompt } from "./common";

console.log("Welcome to the Amazon auth helper!");
console.log("I will need four things from you:");
console.log("- Authorization code from previous step");
console.log("- Client ID");
console.log("- Client secret");
console.log("- Allowed return URL");

prompt("[Press any key to continue]").then(() => {
prompt("Authorization code (from previous step)?").then(authorizationCode => {
prompt("Client ID?").then(clientId => {
prompt("Client secret?").then(clientSecret => {
prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
request.post(
"https://api.amazon.com/auth/o2/token",
{
form: {
grant_type: "authorization_code",
code: authorizationCode,
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectURI,
},
},
(error, response, body) => {
console.dir(JSON.parse(body), { colors: true });
process.exit();
},
);
});
});
});
});
});
43 changes: 43 additions & 0 deletions src/avs-token-helper/avs-token-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// tslint:disable-next-line:no-require-imports
import { prompt } from "./common";

const generateQuery = (params: { [key: string]: string }) =>
Object.keys(params)
.map((key: string) => key + "=" + params[key])
.join("&");

console.log("Welcome to the Amazon auth helper!");
console.log("I will need three things from you:");
console.log("- Client ID");
console.log("- Product ID");
console.log("- Allowed return URL");

prompt("[Press any key to continue]").then(() => {
prompt("Client ID?").then(clientId => {
prompt("Product Id?").then(productId => {
prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
const scopeData = {
"alexa:all": {
productID: productId,
productInstanceAttributes: {
deviceSerialNumber: 123, // can be anything
},
},
};
const getParams = generateQuery({
client_id: clientId,
scope: "alexa:all",
scope_data: JSON.stringify(scopeData),
response_type: "code",
redirect_uri: redirectURI,
});

console.log(
"Great! Copy paste the link below to your browser to authorize your Alexa product",
);
console.log(`https://www.amazon.com/ap/oa?${getParams}`);
process.exit();
});
});
});
});
13 changes: 13 additions & 0 deletions src/avs-token-helper/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function prompt(question: string): Promise<string> {
return new Promise((resolve, reject) => {
const stdin = process.stdin;
const stdout = process.stdout;

stdin.resume();
stdout.write(question + " ");

stdin.once("data", data => {
resolve(data.toString().trim());
});
});
}
15 changes: 15 additions & 0 deletions tsconfig.avs-helper.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./avs-helper",
"sourceMap": false,
"target": "es6",
"strictNullChecks": true,
"declaration": false
},
"include": ["src/avs-token-helper/**/*.ts"],
"exclude": ["./node_modules"]
}