如何在没有用户互动的情况下在Google合作社中对Google服务进行身份验证?
我用以下内容访问我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为可能的解决方案,我可能建议使用
服务帐户
。转到 gcp console
选择您的当前项目(或创建一个新项目):

一旦选择了该项目,请转到
iam和admin
- &gt ;服务帐户
- >+创建服务帐户
:您将被重定向到新的服务帐户页面。注意
电子邮件
字段。您可以共享该电子邮件地址的任何Google Drive内容(与用户个人电子邮件地址相同)。现在您需要创建一个服务帐户密钥,因此您可以使用它来验证您的脚本(选择

JSON
密钥类型):步骤6之后,您应该下载
json
键文件。将其放入您的Google Colab
文件中。您可以做类似的事情来初始化Google客户端(GC
):重要:
gspread
版本应为> = 3.6。 0
支持service_account()
方法。注意:请参阅我的示例以列出所有共享的

Google表
使用gspread
:As a possible solution, I may suggest using
Service Account
.Go to GCP Console
Select your current project (or create a new one):

Once the project is selected, go to
IAM and Admin
->Service accounts
->+ Create service account
: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.
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).Now you need to create a service account key, so you could use it to authenticate your script (select

JSON
key type):After step 6, you should get your
JSON
key file downloaded. Put it into yourGoogle Colab
files. You may do something like that to initialize your google client (gc
) :Important:
gspread
version should be>=3.6.0
to supportservice_account()
method.Note: See my example to list all shared

Google Sheets
usinggspread
: