Google Drive REST API 无需干预的身份验证
GoogleDrive REST API Authentication without intervention
我需要做的是在 GoolgeDrive API 中验证自己的身份(我在 python 中使用它)以访问我的驱动器以执行一些自动任务。
我希望我的脚本 运行 每小时单独运行一次,无需任何干预。所以我需要的是一种使用我的登录名和密码创建凭据对象的方法。
是否可以不做任何重定向等等?
我认为用用户名和密码是不可能实现的。 Google Drive 实现这一点的方式是使用刷新令牌。这意味着您以交互方式验证您的应用程序一次。这使您可以使用一个小时的应用程序。如果正确配置了刷新令牌机制,应用程序将在以后每次需要时更新令牌。
基本上需要进行以下步骤
- 访问console.developers.google.com
- 注册项目并获取 Oauth 客户端 ID
- Select 作为网络服务器类型并输入“http://localhost:8080/”作为授权重定向 URL
- 下载 client_secrets.json 并将其存储在 python 应用程序的根目录中
在相同位置创建名为 "settings.yaml" 的文件,内容如下
client_config_backend: settings
client_config:
client_id: YOUR_CLIENT_ID_GOES_HERE
client_secret: YOUR_CLIENT_SECRET_GOES_HERE
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.son
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.install
在您的 python 代码中,您需要进行适当的身份验证和凭据保存:
gauth = GoogleAuth()
gauth.settings["get_refresh_token"]=True
gauth.settings["approval_prompt"]="force"
if exists(self.credsFile):
gauth.LoadCredentialsFile(self.credsFile)
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
gauth.SaveCredentialsFile(self.credsFile)
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
gauth.Authorize()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile(self.credsFile)
self.driveInstance= GoogleDrive(gauth)
确保将 self.credsFile 作为有效文件名传入
执行此代码应该会在控制台上显示 URL。将其复制到浏览器,进行身份验证并表示同意。 Google 应该向您征求两项同意,第二项是对 Google 驱动器进行身份验证 - 这实际上是由刷新令牌完成的。
一旦获得同意,就会调用开发人员控制台中初始凭据配置的重定向 url。它调用由您的应用程序启动的临时 Web 服务器。这就是回调的完成方式。 (这意味着您必须 运行 浏览器和您的应用程序在同一台机器上执行此步骤 - 您可以将所有三个文件复制到您的服务器)
从现在开始,您的应用应该 运行 永远不需要用户交互。
我需要做的是在 GoolgeDrive API 中验证自己的身份(我在 python 中使用它)以访问我的驱动器以执行一些自动任务。
我希望我的脚本 运行 每小时单独运行一次,无需任何干预。所以我需要的是一种使用我的登录名和密码创建凭据对象的方法。
是否可以不做任何重定向等等?
我认为用用户名和密码是不可能实现的。 Google Drive 实现这一点的方式是使用刷新令牌。这意味着您以交互方式验证您的应用程序一次。这使您可以使用一个小时的应用程序。如果正确配置了刷新令牌机制,应用程序将在以后每次需要时更新令牌。
基本上需要进行以下步骤
- 访问console.developers.google.com
- 注册项目并获取 Oauth 客户端 ID
- Select 作为网络服务器类型并输入“http://localhost:8080/”作为授权重定向 URL
- 下载 client_secrets.json 并将其存储在 python 应用程序的根目录中
在相同位置创建名为 "settings.yaml" 的文件,内容如下
client_config_backend: settings
client_config:
client_id: YOUR_CLIENT_ID_GOES_HERE
client_secret: YOUR_CLIENT_SECRET_GOES_HERE
save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.son
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.install
在您的 python 代码中,您需要进行适当的身份验证和凭据保存:
gauth = GoogleAuth() gauth.settings["get_refresh_token"]=True gauth.settings["approval_prompt"]="force" if exists(self.credsFile): gauth.LoadCredentialsFile(self.credsFile) if gauth.credentials is None: # Authenticate if they're not there gauth.LocalWebserverAuth() gauth.SaveCredentialsFile(self.credsFile) elif gauth.access_token_expired: # Refresh them if expired gauth.Refresh() gauth.Authorize() else: # Initialize the saved creds gauth.Authorize() # Save the current credentials to a file gauth.SaveCredentialsFile(self.credsFile) self.driveInstance= GoogleDrive(gauth)
确保将 self.credsFile 作为有效文件名传入
执行此代码应该会在控制台上显示 URL。将其复制到浏览器,进行身份验证并表示同意。 Google 应该向您征求两项同意,第二项是对 Google 驱动器进行身份验证 - 这实际上是由刷新令牌完成的。
一旦获得同意,就会调用开发人员控制台中初始凭据配置的重定向 url。它调用由您的应用程序启动的临时 Web 服务器。这就是回调的完成方式。 (这意味着您必须 运行 浏览器和您的应用程序在同一台机器上执行此步骤 - 您可以将所有三个文件复制到您的服务器)
从现在开始,您的应用应该 运行 永远不需要用户交互。