如何使用Google Drive Api v3和php显示文本文件的内容?
我想显示存储在Google Drive文件夹中的文本文件的内容。 我正在使用GoogleDriveAPIv3。 目前,我只能显示文件名和MimeType,但我需要的是内容。我想要文本文件文本作为字符串 我找不到合适的函数 到目前为止,这是我代码的一部分如何使用Google Drive Api v3和php显示文本文件的内容?,php,google-drive-api,Php,Google Drive Api,我想显示存储在Google Drive文件夹中的文本文件的内容。 我正在使用GoogleDriveAPIv3。 目前,我只能显示文件名和MimeType,但我需要的是内容。我想要文本文件文本作为字符串 我找不到合适的函数 到目前为止,这是我代码的一部分 function getClient() { $client = new Google_Client(); $client->setApplicationName('Google Drive API PHP Quicksta
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setAuthConfig('credentials.json');
$client->setDeveloperKey('$myApiKey'); // API key
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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 = $myAuthCode;
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
[...]
$file = $service->files->get($fileId);
print "Title: " . $file->getName();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
你能帮我吗?- 您希望使用驱动器API从Google Drive检索文件内容。
- 该文件是文本文件,不是谷歌文档(谷歌文档、电子表格、幻灯片等)
- 您希望通过使用带有php的google api php客户端来实现这一点
- 您已经能够使用驱动API从Google Drive获取值
alt=media
下载文件
修改脚本:
下载文件元数据。因此,为了下载文件内容,请使用$service->files->get($fileId)
$service->files->get($fileId,array(“alt”=>“media”)
https://www.googleapis.com/auth/drive.readonly使用了
。运行脚本时,控制台中将显示用于检索授权代码的URL。因此,请将其放到您的浏览器中并授权范围。并将浏览器的代码输入控制台。通过此操作,将检索访问令牌和刷新令牌,并使脚本正常工作
示例脚本:
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token2.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$service = new Google_Service_Drive($client);
$fileId = "###"; // Please set the file ID of the text file on Google Drive.
// Retrieve file metadata.
$file = $service->files->get($fileId);
print "Title: " . $file->getName();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
// Download file.
$content = $service->files->get($fileId, array("alt" => "media"));
file_put_contents("sample.txt", $content->getBody()); // Please set the filename you want.
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setDeveloperKey('###'); // Please set your API key.
return $client;
}
$client = getClient();
$service = new Google_Service_Drive($client);
$fileId = "###"; // Please set the file ID of the text file on Google Drive.
// Retrieve file metadata.
$file = $service->files->get($fileId);
print "Title: " . $file->getName();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
// Download file.
$content = $service->files->get($fileId, array("alt" => "media"));
file_put_contents("sample.txt", $content->getBody()); // Please set the filename you want.
- 运行此脚本时,将下载该文件并将其另存为文件
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setScopes(Google_Service_Drive::DRIVE_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token2.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} 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);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$service = new Google_Service_Drive($client);
$fileId = "###"; // Please set the file ID of the text file on Google Drive.
// Retrieve file metadata.
$file = $service->files->get($fileId);
print "Title: " . $file->getName();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
// Download file.
$content = $service->files->get($fileId, array("alt" => "media"));
file_put_contents("sample.txt", $content->getBody()); // Please set the filename you want.
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Drive API PHP Quickstart');
$client->setDeveloperKey('###'); // Please set your API key.
return $client;
}
$client = getClient();
$service = new Google_Service_Drive($client);
$fileId = "###"; // Please set the file ID of the text file on Google Drive.
// Retrieve file metadata.
$file = $service->files->get($fileId);
print "Title: " . $file->getName();
print "Description: " . $file->getDescription();
print "MIME type: " . $file->getMimeType();
// Download file.
$content = $service->files->get($fileId, array("alt" => "media"));
file_put_contents("sample.txt", $content->getBody()); // Please set the filename you want.
- 运行此脚本时,将下载该文件并将其另存为文件
我希望文本文件中包含的文本作为字符串,我的第一个示例脚本无法实现他的。怎么样?如果我的第一个示例脚本不是你想要的结果,我必须为我糟糕的英语技能道歉。那时,我可以问你关于你目标的详细信息吗?我想确认一下。顺便问一下,问题仍然存在
是吗?事实上我必须感谢你的支持。即使我的英语不是最好的o很抱歉可能存在误解。我只是在使用您的第一个代码和pattern1的设置,直到昨天晚上,它才给出上述错误…现在,在没有触及任何内容的情况下,它工作正常。因此,是的,您已经实现了我的目标。谢谢