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

时间:2021-12-21 15:48:06

Forgive me if this is a stupid question, novice programmer here.

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

I am trying to use the Soundcloud API to authenticate users in my Rails 3.2.14 app. I'm using the code directly from the Soundcloud developer docs, like so:

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

And when I GET the callback URI:

当我获取回调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)

Everything works fine (I am taken to the Soundcloud page to give my app permissions) but when I am sent back to the callback URI I receive the following error:

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

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

I've obviously googled this and the only suggestion made was that my token is expiring. That doesn't make sense to me because I'm not reusing the access_token later, I'm just doing the initial authentication. Just in case I included the scope => non-expiring parameter when I redirected to Soundcloud, which made no difference.

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

Any suggestions? Has anyone had success integrating Rails with Soundcloud recently? All of the resources I've found seem quite outdated.

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

1 个解决方案

#1


0  

I got this error, when I was not exchanging the :code for the access_token correctly. Check your variables using pry and make sure your :code is not nil

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

its also shows up when the access_token has expired, I know you are passing in :scope => 'non-expiring', but perhaps it loses the parameter at some point

它也会在access_token到期时出现,我知道你传入的内容:scope =>'non-expiring',但也许它在某些时候丢失了参数

#1


0  

I got this error, when I was not exchanging the :code for the access_token correctly. Check your variables using pry and make sure your :code is not nil

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

its also shows up when the access_token has expired, I know you are passing in :scope => 'non-expiring', but perhaps it loses the parameter at some point

它也会在access_token到期时出现,我知道你传入的内容:scope =>'non-expiring',但也许它在某些时候丢失了参数