如何在没有用户互动的情况下在Google合作社中对Google服务进行身份验证?

发布于 2025-02-08 12:35:57 字数 454 浏览 2 评论 0原文

我用以下内容访问我的Google Colab笔记本中的Bouth Google Drive和Google表格:

# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# Google Sheets

from google.colab import auth
auth.authenticate_user()

import gspread
from google.auth import default
creds, _ = default()

gc = gspread.authorize(creds)

from gspread_dataframe import get_as_dataframe, set_with_dataframe

我的问题是它要求用户在弹出窗口中确认身份验证,我想使用ColabCtl(

I use the following to access bouth Google Drive and Google Sheets in my Google Colab Notebook:

# Mount Google Drive
from google.colab import drive
drive.mount('/content/drive')

# Google Sheets

from google.colab import auth
auth.authenticate_user()

import gspread
from google.auth import default
creds, _ = default()

gc = gspread.authorize(creds)

from gspread_dataframe import get_as_dataframe, set_with_dataframe

My problem with this is that it asks for the user to confirm the authentication in a pop-up and I want to use colabctl (https://github.com/bitnom/colabctl) to automate the execution of some scripts, so I need to execute the authentication part without user interaction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2025-02-15 12:35:57

作为可能的解决方案,我可能建议使用服务帐户

  1. 转到 gcp console

  2. 选择您的当前项目(或创建一个新项目):
    “

  3. 一旦选择了该项目,请转到iam和admin - &gt ; 服务帐户 - > +创建服务帐户 “创建服务帐户”

  4. ​用您想要的任何数据填写步骤1。跳过步骤2和3。

  5. 您将被重定向到新的服务帐户页面。注意电子邮件字段。您可以共享该电子邮件地址的任何Google Drive内容(与用户个人电子邮件地址相同)。

  6. 现在您需要创建一个服务帐户密钥,因此您可以使用它来验证您的脚本(选择JSON密钥类型):

  7. 步骤6之后,您应该下载json键文件。将其放入您的Google Colab文件中。您可以做类似的事情来初始化Google客户端(GC):

import gspread

gc = gspread.service_account(filename='/content/credentials.json')

重要: gspread版本应为> = 3.6。 0支持service_account()方法。

注意:请参阅我的示例以列出所有共享的Google表使用gspread

As a possible solution, I may suggest using Service Account.

  1. Go to GCP Console

  2. Select your current project (or create a new one):
    select GCP project

  3. Once the project is selected, go to IAM and Admin -> Service accounts -> + Create service account: create service account

  4. Next, you'll see a create service account prompt, which consists of 3 form steps. Fill in step 1 with any data you want. Skip steps 2 and 3.

  5. You'll be redirected to the new service account page. Note an Email field there. You may share any Google Drive content for that email address (same as you do for users' personal email addresses).

  6. Now you need to create a service account key, so you could use it to authenticate your script (select JSON key type):
    create service account JSON key

  7. After step 6, you should get your JSON key file downloaded. Put it into your Google Colab files. You may do something like that to initialize your google client (gc) :

import gspread

gc = gspread.service_account(filename='/content/credentials.json')

Important: gspread version should be >=3.6.0 to support service_account() method.

Note: See my example to list all shared Google Sheets using gspread:
personal notebook example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文