【问题标题】:Authenticating Rails App w/ Soundcloud, Getting 401 Unauthorized Error: invalid_grant使用 Soundcloud 对 Rails 应用程序进行身份验证,出现 401 未经授权的错误:invalid_grant
【发布时间】:2013-10-18 15:12:54
【问题描述】:

如果这是一个愚蠢的问题,请原谅我,这里是新手程序员。

我正在尝试使用 Soundcloud API 在我的 Rails 3.2.14 应用程序中对用户进行身份验证。我直接使用 Soundcloud 开发者文档中的代码,如下所示:

client = Soundcloud.new(
                    :client_id => 'MY_CLIENT_ID',
                    :client_secret => 'MY_CLIENT_SECRET',
                    :redirect_uri => 'http://localhost:3000/auth/soundcloud/callback'
                    )

# redirect user to authorize URL
redirect_to client.authorize_url(:scope => 'non-expiring')

当我得到回调 URI 时:

client = Soundcloud.new(
                    :client_id => 'MY_CLIENT_ID',
                    :client_secret => 'MY_CLIENT_SECRET',
                    :redirect_uri => 'http://localhost:3000/auth/soundcloud/callback'
                    )

# exchange authorization code for access token
code = params[:code]
access_token = client.exchange_token(:code => code)

一切正常(我被带到 Soundcloud 页面以授予我的应用权限)但是当我被发送回回调 URI 时,我收到以下错误:

SoundCloud::ResponseError at /auth/soundcloud/callback
HTTP status: 401 Unauthorized Error: invalid_grant

我显然已经用谷歌搜索了这个,唯一的建议是我的令牌即将到期。这对我来说没有意义,因为我稍后不会重用 access_token,我只是在进行初始身份验证。以防万一我在重定向到 Soundcloud 时包含了 scope => non-expiring 参数,这没有任何区别。

有什么建议吗?最近有没有人成功地将 Rails 与 Soundcloud 集成?我发现的所有资源似乎都已经过时了。

【问题讨论】:

  • 我在我的应用程序中使用 Soundcloud gem,效果很好。我遵循与您相同的过程,所以我对那个错误感到困惑。当我尝试用 refresh_token 交换 access_token 时遇到了这个错误。但是使用未过期的令牌修复了它。我会检查 client_id 和 client_secret 是否准确。我还会打印出“代码”以检查它是否为空。

标签: ruby-on-rails authentication soundcloud


【解决方案1】:

当我没有正确交换 access_token 的 :code 时,我收到了这个错误。使用pry 检查您的变量并确保您的 :code 不是 nil

它也会在 access_token 过期时显示,我知道你正在传递 :scope => 'non-expiring',但它可能会在某些时候丢失参数

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 2018-07-09
    • 2013-08-09
    相关资源
    最近更新 更多