tejashah88 / node-alexa-smapi

A node.js client library for using the Alexa Skill Management API.
MIT License
13 stars 3 forks source link

Working example #9

Closed trogmaniac closed 5 years ago

trogmaniac commented 5 years ago

Please provive an as-small-as-possible, but complete and working sample. And show how or where to get refreshToken, clientId and clientSecret.

Thanks.

marcelobern commented 5 years ago

Hi @trogmaniac , there you go and have fun!

Minimal node-alexa-smapi Example

Obtaining refreshToken, clientId, and clientSecret:

{
  "refreshToken": "generated by ask util generate-lwa-tokens",
  "clientId": "amzn1.application-oa2-client.myClientId",
  "clientSecret": "myClientSecret"
}

And here is the minimal working code:

const mySmapiClient = require('node-alexa-smapi')();
const mySecrets = require('./secrets.json');

mySmapiClient.tokens.refresh({
  refreshToken: mySecrets.refreshToken,
  clientId: mySecrets.clientId,
  clientSecret: mySecrets.clientSecret,
}).then(() => {
    mySmapiClient.vendors.list().then(result => {
      console.log(`My vendor list: ${JSON.stringify(result, null, ' ')}`);
    })
  });

For More Information

You can see a working example of all API calls in the test suite (test/test_smapi_client.js). This can be run (and will actually create a skill) by following the steps in test/README.md and then executing:

npm install
npm run test-integration

You can read about the test approach used in this short article.

tejashah88 commented 5 years ago

@marcelobern Thank you for the swift reply! It might be worth putting this example in the README!

trogmaniac commented 5 years ago

Thanks a lot for the quick response. Unfortunately creating the credentials at Amazon never worked for me after hours of trying, but at least now i know how it's supposed to work.

marcelobern commented 5 years ago

@tejashah88 thanks for moving this example to the README.md!

@trogmaniac if I may ask, what specific issue did you come across when trying to create the Amazon credentials?

trogmaniac commented 5 years ago

Sorry for the late reply:

1) "ask util generate-lwa-tokens" and using http://127.0.0.1:9090/cb as return url Doesn't work. It looks as if there is no locale service listening on that port.

2) "ask util generate-lwa-tokens --no-browser" and https://s3.amazonaws.com/ask-cli/response_parser.html as return url Gets one step further, but the url shows an error "An unknown scope was requested"

3) "ask util generate-lwa-tokens --scope alexa:all --no-browser" and https://s3.amazonaws.com/ask-cli/response_parser.html as return url Gets even further. It first shows the Amazon Login, then a page with the correct name of my security profile ("proper name" would like access to: " Alexa Voice Services and Alexa Account Connection") But clicking "Allow" only lead to a generic error page with no further information:

"We're sorry!An error occurred when we tried to process your request. Rest assured, we're already working on the problem and expect to resolve it shortly."

I'm stuck here.

I don't know if "--scope alexa:all" is correct, i found it somewhere on the web

I'm also not sure if using "Web settings" for my security profile is correct, because i don't have a website (i only want to use it from the command line) and therefore i left the field "Allowed Origins" empty.

(Edit: incorrectly typed --profile instead of --scope)

marcelobern commented 5 years ago

@trogmaniac thanks for sharing this information.

I am running ask-cli 1.4.2 and just re-tested ask util generate-lwa-tokens and it worked just fine (it opens a webpage asking me to "Click 'Allow' to sign in to SMAPI testing.", where "SMAPI testing" is the name I provided for this auth page).

Here are some details:

Hopefully this will offer some clues on how to get it working for you ;-)

trogmaniac commented 5 years ago

@marcelobern Thanks.

With the scopes you listed i came one step further. I now use this command line:

ask util generate-lwa-tokens --no-browser --scope alexa::ask:skills:readwrite
--scope alexa::ask:models:readwrite --scope alexa::ask:skills:test

With this i come up to a page that shows an authorization code and the message: Please copy the code below and put it into the command prompt:

Unfortunately the command prompt does not accept any keyboard input at this time. So i'm stuck again. (I'm working under Windows)

Edit: I was using the Windows Powershell before. The standard command line window didn't work either. But with Bash for Windows i got one step further.

I will report if i make any progress.

trogmaniac commented 5 years ago

With Bash for Windows i was finally able to generate the refresh token, but running the code from the top (after adding some catch()es) i end up with an "401 unauthorized" error and the message "user has not consented to this operation".

And stuck again...

marcelobern commented 5 years ago

@trogmaniac thanks for sticking with me here and sharing your progress!

Here goes my next best guess on possible issues you might be inadvertently facing...

Please note that there are two tokens in the response:

Make sure to use the refreshToken in the code above.

In case you want to try this using Postman, you can import the JSON below and give it a whirl by going to the Authorization tab and clicking the Get New Access Token button.

And if the error persists, please include sanitized screenshots of the authorization page and the 401 error in your response

Have fun!!!

{
 "info": {
  "_postman_id": "71d1f3b4-9f84-4fe2-9f47-146f71def5eb",
  "name": "Alexa APIs",
  "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
 },
 "item": [
  {
   "name": "Login with Amazon",
   "item": [
    {
     "name": "Refresh token - requires manual user login",
     "request": {
      "auth": {
       "type": "oauth2",
       "oauth2": [
        {
         "key": "addTokenTo",
         "value": "header",
         "type": "string"
        },
        {
         "key": "accessToken",
         "value": "Atza|SOMETHING_GOES_HERE",
         "type": "string"
        },
        {
         "key": "tokenType",
         "value": "bearer",
         "type": "string"
        }
       ]
      },
      "method": "POST",
      "header": [
       {
        "key": "Accept",
        "value": "application/json"
       }
      ],
      "body": {
       "mode": "urlencoded",
       "urlencoded": [
        {
         "key": "grant_type",
         "value": "refresh_token",
         "type": "text"
        },
        {
         "key": "refresh_token",
         "value": "Atzr|SOMETHING_GOES_HERE",
         "type": "text"
        },
        {
         "key": "client_id",
         "value": "amzn1.application-oa2-client.CLIENT_ID",
         "type": "text"
        },
        {
         "key": "client_secret",
         "value": "CLIENT_SECRET",
         "type": "text"
        }
       ]
      },
      "url": {
       "raw": "https://api.amazon.com/auth/o2/token",
       "protocol": "https",
       "host": [
        "api",
        "amazon",
        "com"
       ],
       "path": [
        "auth",
        "o2",
        "token"
       ]
      }
     },
     "response": []
    }
   ]
  }
 ]
}
trogmaniac commented 5 years ago

@marcelobern

Thanks for your continued interest in my problem. I am doing this from and for work and i am on vacation now. I will get back to this in January.

marcelobern commented 5 years ago

@trogmaniac thanks for letting me know and enjoy your time off !!!