Php 将google api集成到dialogflow webhook时删除身份验证
我正在使用Dialogflow创建一个AI助手,我需要使用Google API(Google日历和gmail)。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
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会话通常通过在浏览器中使用会话cookie设置会话ID来实现。助手根本不用饼干。尽管它有一个$\u SESSION
对象,可以以类似的方式使用它,但访问方式却非常不同userStorage
- 在一个纯粹基于web的OAuth方案中,您将引导用户使用Google登录,并作为该过程的一部分,请求授权访问他们的驱动器和GMail。不允许你这么做-它给你的只是一个身份标识
- 您可以使用将助手帐户链接到您的系统,但这需要您拥有一个OAuth服务器,该服务器生成助手将返回给您的身份验证令牌。这是您的OAuth令牌-不是来自Google的令牌(除非您代理它)
- 如果你有一个基于网络的方式来获得授权,你也可以通过助手使用谷歌登录助手