【问题标题】:removing authentication when integrating google api to dialogflow webhook将 google api 集成到 dialogflow webhook 时删除身份验证
【发布时间】:2018-12-16 16:04:40
【问题描述】:

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

这是一个代码sn-p:

$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.";
             }
         }
     }

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

【问题讨论】:

  • 您能否更新您的问题以显示您“将其与 dialogflow webhook 集成”的代码?向我们展示服务器日志也可能指出错误所在。 “错误 500”通常表示服务器代码中存在语法错误。
  • 刚刚添加了代码sn-p

标签: php google-api webhooks dialogflow-es


【解决方案1】:

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

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

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

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

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

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

  • 如果您有基于网络的方式获得授权,您可以通过 Google 助理leverage this to provide access 以及使用 Google 登录来获得授权。

【讨论】:

  • 哎呀,我忽略了这一点。起初我试图在本地托管它(没有代码的对话流部分)并且它有效,所以我基本上只是在与对话流集成时尝试这样做。我会试试这些。谢谢!
猜你喜欢
  • 2018-08-12
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 2019-04-03
  • 2021-07-09
  • 2019-04-28
  • 1970-01-01
  • 2011-05-24
相关资源
最近更新 更多