【问题标题】:Unable to get access token with oauth_verifier, keep getting 401 error无法使用 oauth_verifier 获取访问令牌,不断收到 401 错误
【发布时间】:2012-07-15 22:41:16
【问题描述】:

我看到很多关于同一个问题的问题,但我找不到解决办法,所以我问了一个问题以确保我没有错过任何事情。

在 Twitter 上,我将 callback_url 设置为“https://my_app_url/”。

# consumer
@consumer = OAuth::Consumer.new('consumer_id', 
                                'secret_key',
                                :site => 'https://api.twitter.com')

# request_token
@request_token = @consumer.get_request_token(:oauth_callback => "https://my_app_url/?state=#{state}")

# then get the user to log in via
login_url = @request_token.authorize_url

# I have a params[:oauth_token] and params[:oauth_verifier] in return
# I don't know what the oauth token is for
# I then should be able to get an access token

# all oauth calls are done in an object so @request_token has been conserved.
@access_token = @request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])

# then it happens.
# => 401 Unauthorized

我不知道为什么要提出 401。有没有我错过的步骤? 我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails twitter oauth twitter-oauth


    【解决方案1】:

    get_request_token 为您提供临时 OAuth 令牌以在 OAuth 身份验证流程中对您进行身份验证。 OAuth 验证器是用来证明用户授权应用程序的东西。您必须将其最后提供给 get_access_token 才能获得将用于所有经过身份验证的请求的最终 OAuth 令牌。

    如需更多信息,请考虑阅读官方 Twitter 文档:https://dev.twitter.com/docs/auth/using-oauthhttps://dev.twitter.com/docs/auth/implementing-sign-twitter 用于流程描述)

    【讨论】:

    • 感谢您的解释。我的代码中有遗漏,并且 request_token 未正确保存,因此我从新生成的 request_token 中获取 access_token,该 request_token 与我得到的 oauth_verifier 不匹配。
    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 2021-02-13
    • 2014-05-08
    • 2014-06-11
    • 2013-08-23
    • 1970-01-01
    • 2017-11-17
    • 2022-01-01
    相关资源
    最近更新 更多