[英]Twitter gem authentication error (using omniauth-twitter token/secret)
I am trying to use the Twitter API through the Twitter gem and authenticate the call with the oauth token/secret of an logged on Twitter user previously authorised via omniauth-twitter. 我正在尝试通过Twitter gem使用Twitter API,并使用先前通过omniauth-twitter授权的已登录Twitter用户的oauth令牌/秘密对呼叫进行身份验证。
Authentication for the Twitter user through omniauth-twitter is working fine. 通过omniauth-twitter对Twitter用户进行身份验证工作正常。 Storing the user's token and secret in a model the data is available to the controller through the current_twitter_user.
将用户的令牌和机密存储在模型中,数据可通过current_twitter_user供控制器使用。
The following configuration (application only authentication) is working fine 以下配置(仅应用程序身份验证)运行正常
Twitter.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
config.oauth_token = YOUR_OAUTH_TOKEN
config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end
Twitter.search("something")
Next I wanted to use the authenticated user's oauth_token and oauth_token_secret. 接下来,我想使用经过身份验证的用户的oauth_token和oauth_token_secret。
Following documentation for the Twitter gem I changed the twitter initializer to: 在有关Twitter gem的文档之后,我将twitter初始化程序更改为:
Twitter.configure do |config|
config.consumer_key = YOUR_CONSUMER_KEY
config.consumer_secret = YOUR_CONSUMER_SECRET
end
Then I created an action which includes the following code for test purposes: 然后,我创建了一个包含以下用于测试目的的代码的操作:
if current_twitter_user
tw_client = Twitter::Client.new(
:oauth_token => current_twitter_user.token,
:oauth_token_secret => current_twitter_user.secret
)
@first_result = tw_client.search("something").results.first.text
end
My problem:
我的问题:
The Twitter search call raises an "Twitter::Error::Unauthorized" exception. Twitter搜索呼叫引发“ Twitter :: Error :: Unauthorized”异常。 I checked token and secret provided by current_twitter_user and they seem good.
我检查了current_twitter_user提供的令牌和秘密,它们看起来不错。
Any idea what I should be looking for or what I may be doing wrong? 知道我应该寻找什么或我做错了什么吗? Help/ideas very much appreciated.
帮助/想法非常感谢。
Many thanks, Eugen 非常感谢,欧根
Gems in use: 使用的宝石:
oauth (0.4.7)
omniauth (1.1.4)
omniauth-oauth (1.0.1)
omniauth-twitter (0.0.16)
twitter (4.8.1)
I finally identified the problem (after a night of sleep) - a silly configuration issue! 我终于确定了问题(经过一夜的睡眠)-一个愚蠢的配置问题!
The Twitter gem picked up consumer key/secret pair from the twitter.rb initializer file while omniauth-twitter got it's pair through the environment (sorry, did not show this essential information in my question because that part 'worked'). Twitter gem从twitter.rb初始化程序文件中获取了消费者密钥/秘密对,而omniauth-twitter通过环境获得了它(对不起,在我的问题中未显示此基本信息,因为该部分“有效”了)。
I mixed up Twitter accounts and utilised two different Twitter applications on the consumer key part. 我混合了Twitter帐户,并在消费者关键部分使用了两个不同的Twitter应用程序。 Twitter and omniauth-twitter gems were on different applications.
Twitter和omniauth-twitter宝石在不同的应用程序上。 This approach doesn't (and shouldn't) work and causes authentication failure.
这种方法不起作用(不应该)并导致身份验证失败。 I should have kept the consumer details DRY of course :)
当然,我应该保留消费者详细信息的干燥状态:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.