php - 如何从 API 访问私有(private)电子表格?

标签 php google-spreadsheet-api

您好,我需要从 google 电子表格 API 访问电子表格。我启用了 Google Sheets API 并添加了 API key ,但这不起作用!!!
https://sheets.googleapis.com/v4/spreadsheets/[my_spreadsheet_id]/values/A1?key=[my_api_key]

结果是:
403 - 调用者没有权限

但是,如果我为这个电子表格提供公共(public)访问权限 ---> 它可以工作!!!

结果是:
200 - [正确请求的数据.....]

我的问题是:
如何从 API 访问私有(private)电子表格?

我需要使用诸如 file_get_contents() 之类的 php 函数从我的网站读取和写入订单数据

最佳答案

基本上,您需要获得documentation 中提到的授权。 .

Whenever your application requests private user data, it must send an OAuth 2.0 token along with the request. Your application first sends a client ID and, possibly, a client secret to obtain a token. You can generate OAuth 2.0 credentials for web applications, service accounts, or installed applications.



更多信息来自 OAuth 2.0 documentation ,当使用 OAuth 2.0 访问 Google API 时,所有应用程序都遵循以下步骤:
  • 从 Google API 控制台获取 OAuth 2.0 凭据。
  • 从 Google 授权服务器获取访问 token 。

    Before your application can access private data using a Google API, it must obtain an access token that grants access to that API.

  • 将访问 token 发送到 API。
  • 如有必要,请刷新访问 token 。

  • 最后,为了帮助您使用 PHP 实现,您还可以添加 Authentication and authorization到您的引用列表。希望有帮助!

    关于php - 如何从 API 访问私有(private)电子表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39747381/

    相关文章:

    php - 将 CSV 转换为 PHP 数组 - 结束行问题 (CR LF)

    php - 如何选择具有多个标签的项目

    grails 设计类和外部数据(电子表格)

    java - 使用 Java 的 OAuth 2.0 和 Google Spreadsheet API 的示例是什么?

    php - 替换文本中的单词忽略 url

    php - mysql_fetch_array 中的错误。无法通过 while 循环获取记录

    PHP:数组中的下一个可用值,从非索引值开始

    google-spreadsheet-api - 如何使用 Google Sheets API 自动调整列宽?

    excel - 谷歌电子表格公式获取列中的数字排名

    google-apps-script - 电子表格脚本中的 Typeform 提交触发器