如何在 PHP 中读取 Google Drive 电子表格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14780804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I read a Google Drive Spreadsheet in PHP?
提问by Kiser
All I'm trying to do is read a Google Spreadsheet from a web site. I've read and re-read the Google Drive API docs and everything Google Drive PHP on Stack Overflow and I still can't get to the end zone.
我要做的就是从网站上读取 Google 电子表格。我已经阅读并重新阅读了 Google Drive API 文档以及 Stack Overflow 上的所有 Google Drive PHP,但我仍然无法到达结束区域。
Here's what I've done :
这是我所做的:
- Been to the Google APIs Console and :
- Enabled "Drive API" and "Drive SDK" under 'Services';
- Created an OAuth 2.0 client ID under 'API Access'. Under "Client ID for web applications", the console gave me "Client ID", "Email address", "Client secret", "Redirect URIs" and "JavaScript origins";
- Downloaded the "Google API PHP Client Library";
- Opened the Google Drive document (spreadsheet) and clicked on "Share" to get the document's 'key';
- Set up the following code :
- 去过谷歌 API 控制台和:
- 在“服务”下启用“Drive API”和“Drive SDK”;
- 在“API 访问”下创建了一个 OAuth 2.0 客户端 ID。在“Web 应用程序的客户端 ID”下,控制台给了我“客户端 ID”、“电子邮件地址”、“客户端密码”、“重定向 URI”和“JavaScript 源”;
- 下载了“Google API PHP 客户端库”;
- 打开Google Drive文档(电子表格),点击“分享”获取文档的“key”;
- 设置以下代码:
<?php
session_start();
require_once 'lib/gapi/Google_Client.php';
require_once 'lib/gapi/contrib/Google_DriveService.php';
define( 'GDRIVE_CLIENT_ID', '<API Console - API Access - Client ID>' );
define( 'GDRIVE_CLIENT_SECRET', '<API Console - API Access - Client secret>' );
define( 'GDRIVE_REDIRECT_URIS', '<API Console - API Access - Redirect URIs>' );
define( 'GDRIVE_SCOPE_01', 'h t t p s://www.googleapis.com/auth/drive' );
define( 'GDRIVE_SCOPE_02', 'h t t p s://www.googleapis.com/auth/drive.apps.readonly' );
define( 'GDRIVE_SCOPE_03', 'h t t p s://www.googleapis.com/auth/drive.file' );
define( 'GDRIVE_SCOPE_04', 'h t t p s://www.googleapis.com/auth/drive.metadata.readonly' );
define( 'GDRIVE_SCOPE_05', 'h t t p s://www.googleapis.com/auth/drive.readonly' );
define( 'GDRIVE_FILE_KEY', '<'key' given from 'sharing' document>' );
$client = new Google_Client();
$client->setClientId( GDRIVE_CLIENT_ID );
$client->setClientSecret( GDRIVE_CLIENT_SECRET );
$client->setRedirectUri( GDRIVE_REDIRECT_URIS );
$client->setScopes( array( GDRIVE_SCOPE_01, GDRIVE_SCOPE_02, GDRIVE_SCOPE_03, GDRIVE_SCOPE_04, GDRIVE_SCOPE_05 ) );
try {
$file = $service->files->get( GDRIVE_FILE_KEY );
echo "Title: ", $file->getTitle();
echo "Description: ", $file->getDescription();
echo "MIME type: ", $file->getMimeType();
} catch (Exception $e) {
echo "An error occurred: ", $e->getMessage();
}
?>
All runs fine (no errors anyway) until the $service->files->get( GDRIVE_FILE_KEY )call which triggers the exception:
一切都运行良好(无论如何都没有错误),直到$service->files->get( GDRIVE_FILE_KEY )触发异常的调用:
An error occurred: Error calling GET
https://www.googleapis.com/drive/v2/files: (403) Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
发生错误:调用 GET 时出错
https://www.googleapis.com/drive/v2/files:已超出 (403) 未经身份验证使用的每日限制。继续使用需要注册。
What am I doing wrong? I've pulled my hair out (well, what was left).
我究竟做错了什么?我已经把头发拔了(好吧,剩下的)。
回答by Ben
There is also a much easier, but less clean solution, if you don't want to bother with the API or Google Authentication.
如果您不想打扰 API 或 Google 身份验证,还有一个更简单但不太干净的解决方案。
- Go to your Spreadsheet in Google Drive.
- Go to files -> Publish on the Web
- Select the right worksheet and make sure to enable the checkbox always update.
- Still in the same box you can get a .csv Link to your spreadsheet.
- 转到 Google 云端硬盘中的电子表格。
- 转到文件 -> 在 Web 上发布
- 选择正确的工作表并确保启用复选框始终更新。
- 仍然在同一个框中,您可以获得指向电子表格的 .csv 链接。
You can now access the contents like any other csv File on the Web. Here is some sample code:
您现在可以像访问 Web 上的任何其他 csv 文件一样访问内容。下面是一些示例代码:
$spreadsheet_url="https://docs.google.com/spreadsheet/pub?key=<somecode>&single=true&gid=0&output=csv";
if(!ini_set('default_socket_timeout', 15)) echo "<!-- unable to change socket timeout -->";
if (($handle = fopen($spreadsheet_url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$spreadsheet_data[] = $data;
}
fclose($handle);
}
else
die("Problem reading csv");
回答by Ali Afshar
Please check the Google Drive PHP Quickstart. You have not actually authorized your client. Starting from $authUrl = $client->createAuthUrl();
请查看Google Drive PHP 快速入门。您实际上并未授权您的客户。从...开始$authUrl = $client->createAuthUrl();
All Google Drive requests need authorization of some kind.
所有 Google Drive 请求都需要某种授权。
回答by Juampy NR
I created a sample project that uses a service account to authenticate against Google Spreadsheets in order to access to the contents of a spreadsheet.
我创建了一个示例项目,该项目使用服务帐户对 Google 电子表格进行身份验证,以便访问电子表格的内容。
Have a look at the README at https://github.com/juampynr/google-spreadsheet-reader.
在https://github.com/juampynr/google-spreadsheet-reader查看自述文件。
回答by Asim
You need to use oauth, if you don't google will only allow you to make a few requests.
您需要使用 oauth,如果您不使用,谷歌将只允许您提出一些请求。
If all you want to do is read data out of a google spreadsheet or write data into it then you can just use the spreadsheet api. Check out php-google-spreadsheet-client.
如果您只想从 google 电子表格中读取数据或将数据写入其中,那么您只需使用电子表格 api。查看php-google-spreadsheet-client。
回答by Daan Luttik
If you want you own file to be read you need a service account instead of a "Client ID for web applications". I've been battling this problem myself for way to long and this brought me the sollution: https://developers.google.com/drive/web/service-accounts
如果您希望自己的文件被读取,您需要一个服务帐户而不是“Web 应用程序的客户端 ID”。我自己一直在与这个问题作斗争,这给我带来了解决方案:https: //developers.google.com/drive/web/service-accounts

