gpt4 book ai didi

javascript - 使用由其他人创建且仅共享给特定人员的 API 访问 Google 表格时遇到问题

转载 作者:行者123 更新时间:2023-12-02 22:16:59 26 4
gpt4 key购买 nike

我们的 vendor 维护着一个 Googlesheet,我们正在尝试在我们的网站上添加一个表单来访问该表单并从中提取数据。任务非常简单,但我们在使用 OAuth 时遇到了问题。该工作表不是公开的,仅与某些人(包括我的 Google ID)共享。

现在,我使用 Google Developer Console 来设置 Googlesheets API 库。我添加了一个简单的表单,如下:https://alcocovers.com/knowledge-base/track-your-order/ 。当我第一次使用该表单时,我点击了“登录”按钮(当前隐藏),它在弹出窗口中向我显示了同意书,我使用我的 Google ID 登录,并询问“alcocovers.com 想要访问我的个人资料”信息”,我授予了访问权限。该表格开始工作,我可以从表格中提取信息并显示在我们的网页上。

但问题是,我为链接网站以访问我的 Google 帐户(和 Google 表格)所做的身份验证并不适合所有人。如果我在隐身模式下使用该表单,它将无法访问该工作表,这意味着使用该表单的每个人都必须同意。这没有意义,因为我认为当我第一次登录并授予网站访问我的帐户和工作表的权限时,它将适用于每个人。是网站进行访问,而不是个人用户。我们希望我们网站的用户能够使用该表单并从工作表中提取数据。我怎样才能实现这个目标?

下面是我用来初始化和验证客户端的代码。此代码是从 Google Sheet API 文档复制粘贴的。我假设这需要更改,以便 OAuth 仅发生一次,并且已经将网站链接到工作表,并且在将来使用表单时,不需要同意。

function initClient() {
var API_KEY = ''; // I added api key here

var CLIENT_ID = ''; // I added client id here

var SCOPE = 'https://www.googleapis.com/auth/spreadsheets.readonly';

gapi.client.init({
'apiKey': API_KEY,
'clientId': CLIENT_ID,
'scope': SCOPE,
'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
}).then(function() {
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
}


function handleClientLoad() {
gapi.load('client:auth2', initClient);
}

function updateSignInStatus(isSignedIn) {
if (isSignedIn) {
makeApiCall();
}
}

function handleSignInClick(event) {
gapi.auth2.getAuthInstance().signIn();
}

function handleSignOutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}

更新#1:在尝试了@Jescanellas 建议的解决方案后,我收到以下错误。请注意,我保留了相同的 API key ,仅更改了客户端 ID。 enter image description here

最佳答案

其他用户无法使用您的凭据访问该工作表。为此,您需要使用 Service Account 。创建后,您可以使用您的 Google 帐户通过 Domain-Wide Delegation 对 API 进行授权调用。 ,使用服务帐户凭据而不是您的凭据。请按照文档中的步骤操作:

1 - 创建服务帐户和凭据

  1. Open the Service accounts page. If prompted, select a project.
  2. Click add Create Service Account, enter a name and description for the service account. You can use the default service account ID, or choose a different, unique one. When done click Create.
  3. The Service account permissions (optional) section that follows is not required. Click Continue.
  4. On the Grant users access to this service account screen, scroll down to the Create key section. Click add Create key.
  5. In the side panel that appears, select the format for your key: JSON is recommended.
  6. Click Create. Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. For information on how to store it securely, see Managing service account keys.
  7. Click Close on the Private key saved to your computer dialog, then click Done to return to the table of your service accounts.

2 - 要启用 G Suite 域范围委派:

  1. Locate the newly-created service account in the table. Under Actions, click show more, then Edit.
  2. In the service account details, click Show domain-wide delegation, then ensure the Enable G Suite Domain-wide Delegation checkbox is checked.
  3. If you haven't yet configured your app's OAuth consent screen, you must do so before you can enable domain-wide delegation. Follow the on-screen instructions to configure the OAuth consent screen, then repeat the above steps and re-check the checkbox.
  4. Click Save to update the service account, and return to the table of service accounts. A new column, Domain-wide delegation, can be seen. Click View Client ID, to obtain and make a note of the client ID.

如前所述,使用服务帐户凭据访问工作表。

关于javascript - 使用由其他人创建且仅共享给特定人员的 API 访问 Google 表格时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com