php - 在一个批次中向多个不同的 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 日之后继续工作
我希望我清楚地表达我的担忧。随时询问更多细节。提前致谢 :)
解决方案
您的代码似乎正在向 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。您不会受到更改的影响。
推荐阅读
- java - 如何使用 Selenium 在 Edge 浏览器中启用“麦克风”访问?
- git - Git 卡在“从快照下载”步骤。如何解决?
- python - 计算总月累计订单数
- jquery - jQuery 在另一个图标上切换(脉动)一个图标
- javascript - Vue Draggable 移动到上一个列表
- dart - 有没有办法调用 4 个 API,然后创建一个列表并绘制一个饼图
- c# - 如何检查我的双变量是否为整数?
- excel - 无法在 Excel VBA 中迭代函数返回的对象
- django - 如何在模板中调用模型方法
- google-cloud-dataflow - 如何在预定义模板中禁用公共 IP 以启动数据流作业