将 google api 集成到 dialogflow webhook 时删除身份验证

removing authentication when integrating google api to dialogflow webhook

我正在使用 Dialogflow 创建 AI 助手,我需要使用 Google API(google 日历和 gmail)。 API 运行良好(我使用 this as reference)。但是,当我将我的代码与 dialogflow webhook 集成时,它 returns Webhook Call failed. Error: 500 Internal Server Error.

这是一个代码片段:

$client = new Google_Client();
$client->addScope(Google_Service_Gmail::GMAIL_READONLY); //view your emails and settings
$client->addScope(Google_Service_Gmail::GMAIL_COMPOSE); //manage drafts and send emails
$client->addScope(Google_Service_Gmail::GMAIL_MODIFY);
$client->addScope(Google_Service_Gmail::GMAIL_SEND); //send email on your behalf 
$client->addScope(Google_Service_Calendar::CALENDAR); //manage calendar
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('auto');
if($_SERVER['REQUEST_METHOD'] == 'POST'){
     if(isset($_SESSION['access_token']) && $_SESSION['access_token']){
         $client->setAccessToken($_SESSION['access_token']);
         $gmail = new Google_Service_Gmail($client);
         $user = "emailaddress@gmail.com";

         $calendar = new Google_Service_Calendar($client);

         $intent = $json->queryResult->intent->displayName;
         $location_any = $json->queryResult->parameters->location_any;
         $text = $json->queryResult->parameters->text;
         $name = $json->queryResult->parameters->name;
         $choice = $json->queryResult->parameters->choice;
         $subject = $json->queryResult->parameters->subject;
         $location = $json->queryResult->parameters->location;
         $description = $json->queryResult->parameters->description;
         $startDateTime = $json->queryResult->parameters->startDateTime;
         $endDateTime = $json->queryResult->parameters->endDateTime;

         if($intent== "UnreadMessages"){
             $unread = unReadMessages($gmail, $user);
             $speech = "You have ".$unread." messages";
         }
         else if($intent == "GetSchedule"){
             $events = get_event($calendar);
             if($events){
                $reply = "You have the following events coming up:";
                foreach($events as $event){
                    $eventName = $event['eventName'];
                    $eventTime = $event['time'];
                    $reply .= $eventName."is at".$eventTime;
                }
                $speech = $reply;
             }
             else{
                $speech = "You don't have any events coming up.";
             }
         }
     }

我怀疑这可能是因为每次我尝试 运行 应用程序时都需要身份验证过程,因为只有在我检查会话令牌时才会弹出错误。我现在的问题是如何删除此身份验证过程?

您的方法存在几个严重问题,但其中大部分是由于您将与 Google 助手的交互视为浏览器访问您的网页。尽管 Google 上的 Actions 使用 HTTPS 和 webhook,但它根本不像浏览器。

  • 例如,您检查 $_SESSION 以查看是否已设置访问令牌。 $_SESSION 通常通过在浏览器中使用会话 cookie 设置会话 ID 来实现。 Google 助理根本不使用 cookie。尽管它有一个可以类似方式使用的 userStorage 对象,但访问方式却大不相同。

不清楚这是导致错误的原因,还是问题与此处的某处有关。如果您确实有错误日志,将其包含在问题中会很有用。

还有很多不清楚的地方。主要 - 身份验证令牌首先来自您的代码中的哪里。

  • 在纯基于网络的 OAuth 方案中,您将指示用户使用 Google 登录登录,并在该过程中请求授权以访问他们的云端硬盘和 Gmail。 Google Sign In for the Assistant 不允许你这样做 - 它给你的只是一个身份令牌。

  • 您可以使用 Account Linking 来 link 您系统的助理帐户 - 但这需要您有一个 OAuth 服务器 生成 助手将返回给您的身份验证令牌。这是您的 OAuth 令牌 - 不是来自 Google 的令牌(除非您正在代理它)。

  • 如果您有基于 Web 的方式获得授权,您可以 通过智能助理以及使用 Google 登录智能助理。