我正在尝试使用linkedin gem连接到linkedin API。我有以下代码:
require "rubygems"
require "yaml"
require "linkedin"
config = YAML.load_file("config.yml")
client = LinkedIn::Client.new(config["linkedin"]["consumer_key"], config["linkedin"]["consumer_secret"])
rtoken = client.request_token.token
rsecret = client.request_token.secret
puts client.request_token.authorize_url
puts "Access this URL get the PIN and paste it here:"
pin = gets
puts client.authorize_from_request(rtoken, rsecret, pin)
当我运行此代码时,除了最后一个(authorize_from_request
)之外,每个OAuth步骤都能正常工作。它给出了以下错误:
/Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/consumer.rb:181:in `request': additional_authorization_required (OAuth::Problem)
from /Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/consumer.rb:197:in `token_request'
from /Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/tokens/request_token.rb:18:in `get_access_token'
from /Library/Ruby/Gems/1.8/gems/linkedin-0.2.2/lib/linked_in/authorization_helpers.rb:28:in `authorize_from_request'
from linked_in.rb:17
我的代码遗漏了什么吗?需要什么额外授权?
根据OAuth文档:
additional_authorization_required:The 访问令牌没有正确的 访问范围。
我仍然不明白为什么访问令牌没有正确的访问范围。
由于
答案 0 :(得分:3)
确保在使用之前从引脚中删除尾随空格
puts "Access this URL get the PIN and paste it here:"
pin = gets.strip
puts client.authorize_from_request(rtoken, rsecret, pin)
答案 1 :(得分:0)
此外,除了如何在Linkedin Developer Network中配置应用程序外,不要忘记在配置Omniauth gem初始化程序时设置适当的范围。
尝试添加类似
的内容:scope => 'r_fullprofile r_contactinfo r_emailaddress r_network'
到Omniauth(或Devise)初始化文件中可能存在的现有配置行。
provider :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET']