Ruby 2.2.0谷歌硬盘OAuth刷新
我正在尝试在ruby中设置一个命令行备份应用程序,它使用Oauth访问Google Drive。我已经在帐户中设置了所有内容,创建了我的客户ID和密码。我似乎记得以前这样做过,但找不到代码。在我思考之前,我使用了这个答案: 我已经创建了一个类来处理Google Drive的东西,然后是应用程序主文件,如果将其作为参数“硬”提供,则将执行初始设置,您必须将链接复制并粘贴到web浏览器中,以获取代码,然后您可以将其粘贴到CLI中,以获取初始访问令牌和刷新令牌。这是可行的,遵循这些步骤,我的列表方法(如果没有被注释掉)可以正确地列出Google Drive中的所有内容。当我进行初始设置时,我手动将访问和刷新令牌复制到.access\u令牌和.refresh\u令牌,这些令牌将加载到代码中 不起作用的是刷新令牌,我知道我需要这样做,否则它将过期,这意味着我将不得不再次进行初始设置,这显然是一种痛苦(并且不适合CRON作业)。我在运行@auth.refresh时遇到以下错误Ruby 2.2.0谷歌硬盘OAuth刷新,ruby,oauth-2.0,google-drive-api,Ruby,Oauth 2.0,Google Drive Api,我正在尝试在ruby中设置一个命令行备份应用程序,它使用Oauth访问Google Drive。我已经在帐户中设置了所有内容,创建了我的客户ID和密码。我似乎记得以前这样做过,但找不到代码。在我思考之前,我使用了这个答案: 我已经创建了一个类来处理Google Drive的东西,然后是应用程序主文件,如果将其作为参数“硬”提供,则将执行初始设置,您必须将链接复制并粘贴到web浏览器中,以获取代码,然后您可以将其粘贴到CLI中,以获取初始访问令牌和刷新令牌。这是可行的,遵循这些步骤,我的列表方法(
/home/user/.rvm/gems/ruby-2.2.0/gems/signet-0.6.0/lib/signet/oauth_2/client.rb:947:in `fetch_access_token': Authorization failed. Server message: (Signet::AuthorizationError)
{
"error" : "invalid_grant"
}
from /home/user/.rvm/gems/ruby-2.2.0/gems/signet-0.6.0/lib/signet/oauth_2/client.rb:964:in `fetch_access_token!'
from /home/user/.rvm/gems/ruby-2.2.0/gems/signet-0.6.0/lib/signet/oauth_2/client.rb:981:in `refresh!'
from /home/user/Development/BackupBadger/Sources/Mechanisms/GoogleDriveMechanism.rb:62:in `connect'
from BackupBadger.rb:9:in `<main>'
我的班级谷歌硬盘
module BackupBadger
require 'google/api_client'
require 'google_drive'
require 'pp'
require File.join($sources,'Mechanism.rb')
class GoogleDriveMechanism
def initialize()
@client = Google::APIClient.new(
:application_name => 'Backup Badger',
:application_version => '0.0.1'
)
@access_token_path = File.join($root,'.access_token')
@refresh_token_path = File.join($root,'.refresh_token')
@auth = nil
@access_token = File.open(@access_token_path, "rb").read
@refresh_token = File.open(@refresh_token_path, "rb").read
@session = nil
@client_id = 'CLIENT_ID'
@client_secret = 'CLIENT_SECRET'
@redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
@scope = "https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
end
# Call this to do the initial setup, which requires pasting a url into a web broswer
def hard_setup
@auth = @client.authorization
@auth.client_id = @client_id
@auth.client_secret = @client_secret
@auth.scope = @scope
@auth.redirect_uri = @redirect_uri
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
system'clear'
print "Save your access token\n\n"
print @access_token
print "\nSave your refresh token\n\n"
print @auth.refresh_token
end
def connect
@auth = @client.authorization
@auth.client_id = @client_id
@auth.client_secret = @client_secret
@auth.scope = @scope
@auth.redirect_uri = @redirect_uri
@auth.refresh_token = @refresh_token
puts @access_token
puts @refresh_token
# Error is here
@auth.refresh!
@refresh_token = @auth.refresh_token
@access_token = @auth.access_token
File.write(@refresh_token_path, @refresh_token) if @refresh_token
File.write(@access_token_path, @access_token) if @access_token
puts @access_token
puts @refresh_token
end
def list
@session = GoogleDrive.login_with_oauth(@access_token)
for file in @session.files
p file.title
end
end
end
end
如果是tl;dr只是“如何使用刷新令牌来获取新的访问令牌”,答案是
我不会粘贴代码,因为代码可能会更改,但本质上,您只需发布到Google URL,JSON响应是一个全新的访问令牌。谢谢,我已经看到了这一点。然而,我的问题更具体地针对google drive Ruby gem。这是我的理解,呼吁刷新!方法将获取我的新刷新令牌。这就是我所质疑的。是的,我可以自己写一篇博文,但是为什么当一块宝石存在的时候我会这么做呢。imho的答案是,如果你已经这样做了,你就不必把这个问题贴到SO上去猜测gem是如何工作的,然后希望谷歌将来不要改变这个库来做一些不同的事情。
module BackupBadger
require 'google/api_client'
require 'google_drive'
require 'pp'
require File.join($sources,'Mechanism.rb')
class GoogleDriveMechanism
def initialize()
@client = Google::APIClient.new(
:application_name => 'Backup Badger',
:application_version => '0.0.1'
)
@access_token_path = File.join($root,'.access_token')
@refresh_token_path = File.join($root,'.refresh_token')
@auth = nil
@access_token = File.open(@access_token_path, "rb").read
@refresh_token = File.open(@refresh_token_path, "rb").read
@session = nil
@client_id = 'CLIENT_ID'
@client_secret = 'CLIENT_SECRET'
@redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
@scope = "https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
end
# Call this to do the initial setup, which requires pasting a url into a web broswer
def hard_setup
@auth = @client.authorization
@auth.client_id = @client_id
@auth.client_secret = @client_secret
@auth.scope = @scope
@auth.redirect_uri = @redirect_uri
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
system'clear'
print "Save your access token\n\n"
print @access_token
print "\nSave your refresh token\n\n"
print @auth.refresh_token
end
def connect
@auth = @client.authorization
@auth.client_id = @client_id
@auth.client_secret = @client_secret
@auth.scope = @scope
@auth.redirect_uri = @redirect_uri
@auth.refresh_token = @refresh_token
puts @access_token
puts @refresh_token
# Error is here
@auth.refresh!
@refresh_token = @auth.refresh_token
@access_token = @auth.access_token
File.write(@refresh_token_path, @refresh_token) if @refresh_token
File.write(@access_token_path, @access_token) if @access_token
puts @access_token
puts @refresh_token
end
def list
@session = GoogleDrive.login_with_oauth(@access_token)
for file in @session.files
p file.title
end
end
end
end