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

Php 将google api集成到dialogflow webhook时删除身份验证,php,google-api,webhooks,dialogflow-es,Php,Google Api,Webhooks,Dialogflow Es,我正在使用Dialogflow创建一个AI助手,我需要使用Google API(Google日历和gmail)。API工作得很好(我使用过)。但是,当我将代码与dialogflow webhook集成时,它返回webhook调用失败。错误:500内部服务器错误 以下是一段代码片段: $client = new Google_Client(); $client->addScope(Google_Service_Gmail::GMAIL_READONLY); //view your email

我正在使用Dialogflow创建一个AI助手,我需要使用Google API(Google日历和gmail)。API工作得很好(我使用过)。但是,当我将代码与dialogflow webhook集成时,它返回
webhook调用失败。错误:500内部服务器错误

以下是一段代码片段:

$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助手的交互视为浏览器访问您的网页。虽然谷歌上的操作使用HTTPS和webhook,但在其他方面它根本不像浏览器

  • 例如,检查
    $\u SESSION
    ,查看是否已设置访问令牌<代码>$\u会话通常通过在浏览器中使用会话cookie设置会话ID来实现。助手根本不用饼干。尽管它有一个
    userStorage
    对象,可以以类似的方式使用它,但访问方式却非常不同
目前还不清楚这是否是导致错误的原因,或者问题是否与此相关。如果您确实有错误日志,那么将其包含在问题中会很有用

还有很多问题尚不清楚。主要-在代码中,auth令牌首先来自哪里

  • 在一个纯粹基于web的OAuth方案中,您将引导用户使用Google登录,并作为该过程的一部分,请求授权访问他们的驱动器和GMail。不允许你这么做-它给你的只是一个身份标识

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

  • 如果你有一个基于网络的方式来获得授权,你也可以通过助手使用谷歌登录助手


能否更新您的问题,以显示“将其与dialogflow webhook集成”的代码?向我们显示服务器日志也可能指出错误所在。“Error 500”通常表示服务器代码中存在语法错误。我一开始尝试在本地托管它(没有代码中的dialogflow部分),它工作了,所以我基本上只是在与dialogflow集成时尝试这样做。我来试试这些。谢谢