【发布时间】:2015-06-19 15:26:57
【问题描述】:
我正在尝试使用 oauth2 ruby Gem (https://github.com/intridea/oauth2) 离线访问 Google 联系人。我当前的代码是:
#!/usr/bin/env ruby
require 'oauth2'
require 'yaml'
require 'pp'
auth = YAML.load_file('.auth.yml')
client_id = auth['google']['clientid']
client_secret = auth['google']['secret']
redirect = 'urn:ietf:wg:oauth:2.0:oob'
code = ARGV[0]
token = nil
client = OAuth2::Client.new(client_id, client_secret, site: 'https://accounts.google.com', token_url: '/o/oauth2/token', authorize_url: '/o/oauth2/auth')
if code
token = client.auth_code.get_token(code, :redirect_uri => redirect)
auth['google']['token'] = token.to_hash
open('.auth.yml', 'w'){|f| f.write(auth.to_yaml)}
elsif auth['google']['token']
token = OAuth2::AccessToken.from_hash(client, auth['google']['token'])
token.refresh! if token.expired?
auth['google']['token'] = token.to_hash
open('.auth.yml', 'w'){|f| f.write(auth.to_yaml)}
else
puts client.auth_code.authorize_url(scope: 'https://www.google.com/m8/feeds', redirect_uri: redirect, access_type: :offline)
end
pp token.expired? if token
我可以使用它来获取一个访问令牌,有效期为一小时,当我提交从生成的 URL 的确认屏幕获得的代码时,我确实在收到的响应中看到了一个 refresh_token,但只有确认屏幕提示我访问我的联系人,它不会询问我是否要离线访问,并且令牌确实会在一小时后过期。我做错了什么?
【问题讨论】:
标签: ruby oauth-2.0 google-contacts-api