如何发送401请求以取消与操作之间的Google帐户关联

时间:2019-07-09 19:46:59

标签: javascript dialogflow

我正在尝试允许我的Google操作的用户通过语音(例如说“取消链接”)取消其帐户的链接。这样,当他们再次使用该操作时,将提示他们重新链接其帐户。

我尝试返回401状态代码,但是在日志中收到错误“发送后无法设置标题”。当用户说“取消链接”时,将触发以下功能。

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {SignIn} = require('actions-on-google');

process.env.DEBUG = 'dialogflow:*'; // Enable lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({request: request, response: response});

  var accessToken = request.body.originalDetectIntentRequest.payload.user.accessToken;
  oauth2Client.setCredentials({
    access_token: accessToken
  });  

  function welcome(agent) {
    const conv = agent.conv();
    conv.ask(new SignIn('To get your account details'));
    agent.add(conv);
  }

  function signInCheck(agent) {
    const conv = agent.conv();
    const granted = conv.arguments.get('SIGN_IN').status === 'OK';

    if(granted){
      oauth2Client.setCredentials({
        access_token: accessToken
      }); 
      agent.add(`Welcome.`);
    } 
  }

  function unlinkAccount(agent) {
    agent.add(`OK. I unlinked your account.`);

    response.status(401).send("I unlinked your account.");
    return;
  }

});

0 个答案:

没有答案