2011-06-08 60 views
2

我想使用linkedin gem連接到linkedin API。我有以下代碼:使用linkedin紅寶石gem的OAuth問題

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) 

當我運行這段代碼,每一步的OAuth工作正常,除了最後一個(authorize_from_request)。它給出了以下錯誤:

/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 access token does not have the correct access scopes.

我還是不明白,爲什麼訪問令牌不正確的訪問範圍。

感謝

回答

3

確保您從[銷尾隨空白使用它

puts "Access this URL get the PIN and paste it here:" 
pin = gets.strip 

puts client.authorize_from_request(rtoken, rsecret, pin) 
+0

我怎麼會錯過!謝謝分配;) – rogeriopvl 2011-07-08 11:04:59

+0

不用擔心 - 我其實也有同樣的問題:) – 2011-07-08 16:20:33

0

另外,不要忘記配置Omniauth寶石初始化時設定適當的範圍除了如何在App之前已在Linkedin開發者網絡中配置。

嘗試添加類似

:scope => 'r_fullprofile r_contactinfo r_emailaddress r_network' 

你可能在Omniauth(或設計)初始化文件中的現有配置行。

provider :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET']