使用Soundcloud验证Rails应用程序,获取401 Unauthorized错误:invalid_grant

时间:2013-10-18 15:12:54

标签: ruby-on-rails authentication soundcloud

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

我正在尝试使用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

我显然是用Google搜索过的,唯一的建议就是我的令牌即将到期。这对我没有意义,因为我之后没有重用access_token,我只是在做初始身份验证。以防我包括范围=>我重定向到Soundcloud时的非过期参数,没有任何区别。

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

1 个答案:

答案 0 :(得分:0)

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

当access_token过期时也显示出来,我知道你传入的内容:scope => '非过期',但也许它会在某个时候失去参数