Php 升级Google Drive API范围时如何重新验证
我有一个web应用程序,可以访问Google Drive(TeamDrive文件夹)。它在WordPress和PHP上运行。它读取驱动器的内容,显示文件夹/文件,并允许下载文件。一切正常,但现在我需要添加一个文件上传功能。当前作用域是DRIVE_READONLY,需要升级到允许写入(文件创建)功能的驱动器 我在代码中将作用域更改为DRIVE everywhere,并在谷歌控制台的OAuth同意屏幕中添加了auth/DRIVE作用域。然而,凭证JSON文件中的作用域表示“https://www.googleapis.com/auth/drive.readonly". 当试图上传一个文件时,GoogleAPI响应错误“权限不足:请求的身份验证范围不足”。我想问题出在json中,我需要再次进行身份验证,以将凭据升级到新范围 当我有一个在生产环境中运行的应用程序时,我该怎么做?我找不到任何指导如何做。我是否应该在控制台中,通过使用应用程序或CLI中使用的用户登录,或者如何登录Php 升级Google Drive API范围时如何重新验证,php,google-drive-api,Php,Google Drive Api,我有一个web应用程序,可以访问Google Drive(TeamDrive文件夹)。它在WordPress和PHP上运行。它读取驱动器的内容,显示文件夹/文件,并允许下载文件。一切正常,但现在我需要添加一个文件上传功能。当前作用域是DRIVE_READONLY,需要升级到允许写入(文件创建)功能的驱动器 我在代码中将作用域更改为DRIVE everywhere,并在谷歌控制台的OAuth同意屏幕中添加了auth/DRIVE作用域。然而,凭证JSON文件中的作用域表示“https://www.g
代码是PHP,它使用Google API客户端和类,这些类在读取驱动器时运行良好,对驱动器上的内容进行搜索。您必须从命令行运行代码/脚本。当代码找不到凭据文件(您首先删除了该文件)时,它会执行此操作。当您在浏览器中打开链接并将其重定向到URL(创建密钥时在Google API控制台中设置)时,您会发现添加到URL末尾的验证码。将其复制粘贴到命令行,代码将获取凭据文件并将其保存到硬盘
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Web client file upload');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig('inc/client_secret_xxxxxx.apps.googleusercontent.com.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('inc/credentials_xxxxxx.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
您必须从命令行运行代码/脚本。当代码找不到凭据文件(您首先删除了该文件)时,它会执行此操作。当您在浏览器中打开链接并将其重定向到URL(创建密钥时在Google API控制台中设置)时,您会发现添加到URL末尾的验证码。将其复制粘贴到命令行,代码将获取凭据文件并将其保存到硬盘
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Web client file upload');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setAuthConfig('inc/client_secret_xxxxxx.apps.googleusercontent.com.json');
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory('inc/credentials_xxxxxx.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
您是否在添加新的作用域后创建了新的凭据,然后导出并使用了新的JSON?您是指在Google API控制台中创建的客户端ID和客户端机密?不,我没有那样做。我假设这些仍然有效,并且需要使用新的作用域刷新令牌。明白了,你说得对。在这种情况下,您确定删除了在使用
drive\u readonly
时创建的token.json
?如果没有,应用程序将不会创建新的,而是使用旧的。当我删除包含令牌的json时,应用程序将停止工作。它无法访问GDrive,并且没有显示任何内容。它也不会创建新的令牌。如果我撤消删除,则它再次正常工作,但作用域是只读的。您可以共享您正在使用的代码吗?您是否在添加新作用域后创建了新凭据,然后导出并使用了新的JSON?您是指在Google API控制台中创建的客户端ID和客户端密码?不,我没有那样做。我假设这些仍然有效,并且需要使用新的作用域刷新令牌。明白了,你说得对。在这种情况下,您确定删除了在使用drive\u readonly
时创建的token.json
?如果没有,应用程序将不会创建新的,而是使用旧的。当我删除包含令牌的json时,应用程序将停止工作。它无法访问GDrive,并且没有显示任何内容。它也不会创建新的令牌。如果我撤消删除,则它再次正常工作,但作用域为只读。您可以共享您正在使用的代码吗?