首页 > 解决方案 > 在一个批次中向多个不同的 API 发送请求

问题描述

我收到一封电子邮件,说明我的 Google Cloud 项目使用了计划于 2019 年 3 月 25 日被拒绝的 Global HTTP Batch 服务端点。

经过一番研究,我在这里发现了一些可能相关的东西,

“正在停止的是在一个批次中向多个不同的 API 发送请求......

所以我怀疑这是因为我在 Google Cloud 项目中的代码:

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$client->addScope(Google_Service_Drive::DRIVE);
print_r($client);

//create new spreadsheet
$sheetService = new Google_Service_Sheets($client);
$requestBody = new Google_Service_Sheets_Spreadsheet([
                  'properties' => [
                      'title' => $new_sheet_title
                  ]
               ]);
$response = $sheetService->spreadsheets->create($requestBody);
$spreadsheetId = $response->spreadsheetId;

$driveService = new Google_Service_Drive($client);

$client->setUseBatch(true);
$batch = $driveService->createBatch();

$userPermission = new Google_Service_Drive_Permission(array(
                      'type' => 'user',
                      'role' => 'writer',
                      'emailAddress' => 'mypersonalemail@gmail.com'
                  ));
$request = $driveService->permissions->create(
              $spreadsheetId, $userPermission, array('fields' => 'id'));
$batch->add($request, 'user'); 
$results = $batch->execute();  
$client->setUseBatch(false); 

//then no longer using Drive API, only using Sheet API to do formatting...

当我 print_r() 我的 $client 时,我看到了这个:

[requestedScopes:protected] => Array
    (
        [0] => https://www.googleapis.com/auth/spreadsheets
        [1] => https://www.googleapis.com/auth/drive
    )

我正在使用Google Sheet API创建电子表格并使用Drive API来允许我的个人电子邮件的权限(嗯,为什么我使用这种方式是另一个故事......)顺便说一下,我正在使用Google 客户端库(v2 .2.1) php 测试版

所以我想我要做的是

$client = new Google_Client();
$client->useApplicationDefaultCredentials();

//using setScopes to overwrite original scopes
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$sheetService = new Google_Service_Sheets($client);
//create my spreadsheet

$client->setScopes(Google_Service_Drive::DRIVE);
$driveService = new Google_Service_Drive($client);
//allow permission to my personal email

//then set it back & do formatting to my spreadsheet
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$sheetService = new Google_Service_Sheets($client);

...

我测试并确定它有效

所以我的问题是:
1. 我的代码的哪一部分正在向 Sheet 和 Driver API 发送批处理请求?当我检查我的批量更新请求时,没有找到调用这两个 API 的请求。(参考这里

2. 我不确定我的新脚本是否能解决问题。由于 Google 尚未拒绝服务,所以我不确定我的新脚本是否会在 2019 年 3 月 25 日之后继续工作

我希望我清楚地表达我的担忧。随时询问更多细节。提前致谢 :)

标签: phpgoogle-apigoogle-drive-apigoogle-api-php-clientgoogle-sheets-api

解决方案


您的代码似乎正在向 Google drive api 发送批处理请求。

$client->setUseBatch(true);
$batch = $driveService->createBatch();

$userPermission = new Google_Service_Drive_Permission(array(
                      'type' => 'user',
                      'role' => 'writer',
                      'emailAddress' => 'mypersonalemail@gmail.com'
                  ));
$request = $driveService->permissions->create(
              $spreadsheetId, $userPermission, array('fields' => 'id'));
$batch->add($request, 'user'); 
$results = $batch->execute();  
$client->setUseBatch(false); 

您在此批处理请求中的所有请求似乎都将发送到 google Drive api。我根本看不到任何迹象表明您在其中混合了 api。您不会受到更改的影响。


推荐阅读