我正在rails 5上开发一个个人soundcloud应用程序,而oauth给我带来了一些麻烦。我可以重定向到soundcloud的应用程序并向我的用户授予权限。当我尝试在重定向uri上将代码交换为令牌时,会得到一个错误我已经包括了我的代码和下面的错误图像。

def connected

  client = Soundcloud.new(:client_id => 'My ID',
                      :client_secret => 'my secret',
                      :redirect_uri => "http://localhost:3000/login/soundcloud/callback")

  code = params[:code]
  value = client.exchange_token(:code => code) #get an error on this line
 #my code to save access token into db goes here.
end

我添加了此图像
ruby-on-rails - Ruby on Rails-SoundCloud OAuth 2未定义方法`access_token'-LMLPHP
因为我犯的错误我想可能会更有帮助。

最佳答案

我遇到了同样的问题,所以我只是为这个步骤手动创建了请求。Soundcloud gem已经过时了,我相信这和它有关。

    @client = Soundcloud.new(client_id: client_id,
                             client_secret: client_secret,
                             redirect_uri:'http://localhost:3000/soundcloud/connected')
    @code = params[:code]
    response = HTTParty.post("https://api.soundcloud.com/oauth2/token",
    body: {
      "client_id" => client_id,
      "client_secret" => client_secret,
      "redirect_uri" => 'http://localhost:3000/soundcloud/connected',
      'grant_type'=> 'authorization_code',
      "code" => @code
      }
  )
  access_token = response["access_token"]
  @authed_client = Soundcloud.new(access_token: access_token)
  soundcloud_user = @authed_client.get('/me')
end

希望这有帮助。

关于ruby-on-rails - Ruby on Rails-SoundCloud OAuth 2未定义方法`access_token',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38818240/

10-13 02:37