在 Google 电子表格 API 中使用 OAuth 2.0 授权请求 [英] Authorizing requests with OAuth 2.0 in Google Spreadsheet API

查看:24
本文介绍了在 Google 电子表格 API 中使用 OAuth 2.0 授权请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 PHP 网页,该网页需要从我域中的 google 电子表格中读取一些数据(我使用的是 Google Apps 免费版).

I am trying to create a PHP web page that requires reading some data from a google spreadsheet in my domain (I am using Google Apps Free Edition).

要阅读的电子表格是非公开的,但对我域中的某些人可见.由于它是非公开的,我知道即使我使用 API 读取它也会有一些身份验证和授权的东西.

The spreadsheet to be read is a non-public one but is visible to some people in my domain. Since it's non-public, I know there will be some authentication and authorization stuff even I am using the API to read it.

我找到了这个页面,但有一些我不明白的地方:http://code.google.com/intl/zh-TW/apis/spreadsheets/data/3.0/developers_guide.html#Auth

I found this page but there is something that I don't understand: http://code.google.com/intl/zh-TW/apis/spreadsheets/data/3.0/developers_guide.html#Auth

它说我们应该使用 OAuth 2.0 协议,这没问题.但它还表示,在授权过程中,Google 会向用户显示一个 OAuth 对话框,要求他们授权您的应用程序请求他们的一些数据.".

It says we should use OAuth 2.0 protocol, this is ok. But it also says during the authorizing process, "Google displays an OAuth dialog to the user, asking them to authorize your application to request some of their data.".

我的网页将显示从电子表格中读取的一些数据.因此,每当有人访问我的网页时,它都会向电子表格所有者显示一个对话框,请求许可?是这个意思吗?

My webpage will display some data read from the spreadsheet. So whenever somebody goes to my webpage, it will display a dialog to the spreadsheet owner asking for permission? Is this what it means?

非常欢迎任何建议.

推荐答案

您实际尝试完成的是您的服务器和 Google 之间的服务器到服务器身份验证.

What you're actually trying to accomplish is Server to Server authentication between your server and Google.

这样,当访问者进入您的页面时,您将从您自己的电子表格中获取数据,而无需任何第三方参与.

This way, when a visitor enters your page/s you'll grab data from your own spreadsheet, without any 3rd party involvement.

我不可能在 Google 服务帐户,以及此处

还有,另一种解决方案(更容易实现,但可能会遇到一些挫折)是将 oauth 2.0 协议与您的 Google 开发帐户(从 Google Console API 检索)一起使用.

Also, another solution (which is much easier to accomplish, but might have some set backs) is to use the oauth 2.0 protocol with your Google dev account (retrieved from Google Console API).

  1. 如果您还没有,请创建一个 Google Dev 帐户(Google Console API)
  2. 使用离线"授权为您的应用程序生成访问/刷新令牌 - 这意味着即使您没有使用电子表格帐户登录,您也可以使用开发帐户向电子表格帐户发出 API 请求.
  3. 保存您生成的刷新令牌,并使用它一遍又一遍地生成访问令牌(访问令牌过去 1 小时).

刷新令牌不应该过期,但如果它会过期,您可以随时再次生成它并用新的替换您拥有的令牌,并继续用它生成访问令牌.

Refresh token are not supposed to expire, but in case it would, you can always generate it again and replace the one you had with a new one, and keep generating access tokens with it.

主要的障碍是,如果您的刷新令牌失效,您必须手动替换它,因为它需要您重新授予对开发帐户的访问权限以访问电子表格帐户.

The major set back is in case your refresh token gets invalidated, you'll have to manually replace it, as it will require you to re-grant access to your dev account to access your spreadsheet account.

我希望这会有所帮助.

男人

这篇关于在 Google 电子表格 API 中使用 OAuth 2.0 授权请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆