在单个批处理中向多个不同的 API 发送请求



我收到一封电子邮件,指出我的 Google Cloud 项目使用了全局 HTTP Batch 服务端点,这些端点计划于 2019 年 3 月 25 日关闭。

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

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

所以我怀疑这是因为我在谷歌云项目中的代码:

$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
)

我正在使用谷歌表格API来创建电子表格,并使用Drive API允许允许我的个人电子邮件(好吧,为什么我以这种方式使用是另一回事...... 顺便说一下,我正在使用谷歌客户端库(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。我的代码的哪一部分同时向工作表和驱动程序 API 发送批处理请求?当我检查我的批量更新请求时,没有找到调用两个 API 的请求。(参考这里(

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

我希望我清楚地表达我的关切。请随时询问更多详细信息。 提前致谢:)

您的代码似乎正在向 Google 云端硬盘 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 云端硬盘 API。 我根本没有看到任何迹象表明您在其中混合了 API。 您不会受到更改的影响。

最新更新