1

我已引用此链接https://medium.com/coinmonks/link-your-amazon-alexa-skill-with-a-google-api-within-5-minutes-7e488dc43168并使用与所述相同的配置。

我能够在 lambda 函数中获取访问令牌 var accesstoken =handlerInput.requestEnvelope.context.System.user.accessToken;

如何通过配置 alexa 开发者控制台帐户链接部分在 handlerinput 事件中获取刷新令牌?

我试过在配套应用程序中启用/禁用技能,用模拟器测试,从谷歌自动访问中删除 alexa 技能,然后允许访问。

LaunchRequestHandler = {
  canHandle(handlerInput) {

    return handlerInput.requestEnvelope.request.type === 'LaunchRequest' || (handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'LaunchRequest');
  },
  async handle(handlerInput) {


    console.log('LAUNCH REQUEST CALLED');
    const speechText = 'Welcome!';
    if (handlerInput.requestEnvelope.context.System.user.accessToken === undefined) {
      console.log('ACCESS TOKEN NOT FOUND IN LAUNCH REQUEST');
      return handlerInput.responseBuilder
        .speak("ACCESS TOKEN NOT FOUND IN LAUNCH REQUEST")
        .reprompt("ACCESS TOKEN NOT FOUND IN LAUNCH REQUEST")
        .withLinkAccountCard()
        .withShouldEndSession(true)
        .getResponse();
    } 



     const fs = require('fs');
    const readline = require('readline');
    const { google } = require('googleapis');


    const SCOPES = ['https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile','https://www.googleapis.com/auth/plus.me','https://www.googleapis.com/auth/tasks.readonly','https://www.googleapis.com/auth/tasks'];

function authorize() {

      return new Promise((resolve) => {
        const client_secret = process.env.client_secret;
        const client_id = process.env.client_id;
        const redirect_uris = ['*******************************', '*******************************', '*******************************'];
        const oAuth2Client = new google.auth.OAuth2(
          client_id, client_secret, redirect_uris[0]);

        console.log('access token found : ' + handlerInput.requestEnvelope.context.System.user.accessToken);

        oAuth2Client.credentials = { "access_token": handlerInput.requestEnvelope.context.System.user.accessToken };
4

1 回答 1

1

刷新令牌不会被 Alexa 暴露给技能,换句话说:您的技能代码无法访问刷新令牌,这完全由 Alexa 管理。当您的客户将访问您的技能并且访问令牌即将到期时,Alexa 将在幕后使用刷新令牌向您的身份提供者(在您的情况下为 Google)询问新令牌。

这在https://developer.amazon.com/docs/account-linking/account-linking-for-custom-skills.html#choose-auth-type-overview的 Alexa 帐户链接文档中进行了解释

于 2019-01-30T16:01:48.953 回答