如何通过在 alexa 开发人员控制台中配置帐户链接部分,在 lambda 函数中获取谷歌 oauth 刷新令牌



我已经 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 开发人员控制台帐户链接部分在处理程序输入事件中获取刷新令牌?

我尝试在配套应用程序中启用/禁用技能,使用模拟器进行测试,从谷歌自动访问中删除 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 };

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

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

最新更新