【问题标题】:Sending requests to multiple different APIs in a single batch在一个批次中向多个不同的 API 发送请求
【发布时间】:2019-03-23 23:59:23
【问题描述】:

我收到一封电子邮件,说明我的 Google Cloud 项目使用了计划于 2019 年 3 月 25 日关闭的全局 HTTP 批处理服务端点。

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

“正在停止的是在一个批次中向多个不同的 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 Client Library (v2.2.1) php beta

所以我想我要做的是

$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 的请求。 (参考here

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

我希望我清楚地表达我的担忧。随时询问更多细节。 在此先感谢:)

【问题讨论】:

    标签: php google-api google-drive-api google-api-php-client google-sheets-api


    【解决方案1】:

    您的代码似乎正在向 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。您不会受到更改的影响。

    【讨论】:

    • 我也是这么想的。正如谷歌发送电子邮件说我的项目正在使用明年将拒绝的全球 HTTP 批处理端点。据我所知,我的项目没有使用任何这些,而是​​使用 Google API 客户端库来调用 Google API。所以我没什么好担心的吧?
    • 当它第一次出现时,我跑了一遍并检查了很多客户端库。他们中的大多数人已经更改了他们的代码。其他人在这个过程中。你应该很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2019-12-11
    相关资源
    最近更新 更多