如何通过在Alexa开发人员控制台中配置帐户链接部分来在lambda函数中获取Google oauth刷新令牌?

时间:2019-01-30 11:08:57

标签: oauth alexa alexa-skills-kit google-tasks

我已经引用了此链接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事件中获取刷新令牌?

我尝试在伴侣应用中启用/禁用技能,经过模拟器测试,从Google自动访问中删除了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 };

1 个答案:

答案 0 :(得分:1)

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

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