【发布时间】:2015-07-24 19:53:04
【问题描述】:
所以在更新之前,有一种简单的方法可以登录到 google drive 并操作您的 google 文档 - 使用 ruby。
在您能够使用此登录到您的谷歌驱动器之前。
require 'google_drive'
$session = GoogleDrive.login("email@gmail.com", "password")
但现在您收到警告消息:
WARNING: GoogleDrive.login is deprecated and will be removed in the next version. Use GoogleDrive.login_with_oauth instead.
所以我去了 git hub page 看看 google 希望我们如何使用高级语言的服务,并发现他们希望我们使用 oAuth2。像这样。
require 'google/api_client'
require 'google_drive'
client = Google::APIClient.new(
:application_name => 'Example Ruby application',
:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token
$session = GoogleDrive.login_with_oauth(access_token)
这很好,但我无法保存 auth 变量。 我想在脚本中对此进行硬编码,这样我就不必继续去谷歌获取新的 access_token。
所以我尝试获取 access_token 并将其添加到脚本中。
require 'google/api_client'
require 'google_drive'
client = Google::APIClient.new
access_token = "TokenFromGoogleHardCoded"
$session = GoogleDrive.login_with_oauth(access_token)
# $session doesn't connect
我不确定我是否在正确的庄园中解决了这个问题。我想保存完整的 auth 变量并将其硬编码到我的脚本中。
【问题讨论】:
标签: ruby oauth google-drive-api