首页 > 解决方案 > 无法使用 PHP 在 google apis 请求中添加多个范围

问题描述

我正在研究 google api 并访问 google 驱动器日历等,同时请求单个范围,即 google 表格或文档,它工作正常,但无法在单个请求中设置多个范围。

我尝试了其他论坛上讨论的各种事情,例如添加多个,例如使用逗号分隔添加多个范围

$client->setScopes(Google_Service_Calendar::CALENDAR,Google_Service_Drive::DRIVE);

还创建了新数组,例如

$scopes=['Google_Service_Calendar::CALENDAR','Google_Service_Drive::DRIVE']

但没有一个对我有用

这是我的代码

$client = new Google_Client();
$client->setScopes(Google_Service_Calendar::CALENDAR,Google_Service_Drive::DRIVE);
$client->setAuthConfig('simplo_other_secret.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

任何帮助,将不胜感激

标签: phpgoogle-apigoogle-workspace

解决方案


我不是PHP专家,但是...

根据示例, setScopes 方法确实可以接受范围数组作为参数。请参阅:https ://hotexamples.com/examples/-/Google_Client/setScopes/php-google_client-setscopes-method-examples.html

您的示例代码显示您确实尝试传入一个数组,但是您将Google_Service_Calendar::CALENDARGoogle_Service_Drive::DRIVE包裹在单引号中,这会将它们转换为字符串,而不是对它们所代表的值的引用。


推荐阅读