我正在尝试在ruby中设置命令行备份应用程序,该应用程序使用Oauth访问Google云端硬盘。我已在帐户中设置了所有内容,创建了我的客户端ID和密码。我似乎记得以前这样做但找不到代码。我在考虑之前使用过这个答案:Ruby google_drive gem oAuth2 saving
我已经创建了一个类来处理Google Drive的东西然后有应用程序主文件,如果给出“硬”作为参数将执行初始设置,您必须将链接复制并粘贴到Web浏览器中获取一个代码,然后将其粘贴到CLI中以获取初始访问令牌和刷新令牌。这是有效的,按照这些步骤,我的列表方法(未注释掉时)正确列出了Google云端硬盘中的所有内容。当我进行初始设置时,我手动复制访问权限并刷新令牌到.access_token和.refresh_token,这些都在代码中加载。
什么不起作用,刷新令牌,我理解我需要做,否则它会过期,这意味着我将不得不再次进行初始设置,这显然是一个痛苦(并不适合CRON工作)。运行@ auth.refresh时出现以下错误!
/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>'
我已经看过它可能是什么了但是我暂时坚持为什么这个错误在我看似可以验证时触发(因为我可以列出驱动器上的所有文件)
我的主要文件
$root=File.join('/home/user/Development/BackupBadger')
$sources=File.join($root,'Sources')
require File.join($sources,'Mechanisms','GoogleDriveMechanism.rb')
badger = BackupBadger::GoogleDriveMechanism.new
if ARGV[0] == "hard" then
badger.hard_setup
else
badger.connect
#badger.list
end
我的Google云端硬盘
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
答案 0 :(得分:0)
如果tl; dr只是&#34;我如何使用刷新令牌来获取新的访问令牌#34;,那么答案是https://developers.google.com/accounts/docs/OAuth2WebServer#refresh
我不会粘贴代码,因为它可能会更改,但实际上,您只需发布到Google网址,而JSON响应就是一个闪亮的新访问令牌。