试图实现SoundCloud api无法找到访问令牌

时间:2014-07-17 20:11:38

标签: ruby-on-rails ruby api soundcloud access-token

我已经能够让用户使用soundcloud登录,但是在他们登录后,我无法访问他们的信息,此时此刻我相信我需要一个access_token。我不清楚我会在哪里获得此访问令牌。在我的测试中,我只想尝试访问自己的帐户

users_controller.rb

class UsersController < ApplicationController 
require 'soundcloud'
    def index

    end


    def new
        client = Soundcloud.new(:client_id => "CLIENT_ID",
                        :client_secret => "CLIENT_SECRET",
                        :scope => "non-expiring",
                        :redirect_uri => 'http://localhost:3000/users/show')

        redirect_to client.authorize_url()
    end 

    def show 
        client = Soundcloud.new(:access_token => 'ACCESS_TOKEN_SHOULD_GO_HERE')
        puts client.get('/me').username
    end
end

不确定我应该提供的应用程序的其他信息相当薄,但任何帮助将不胜感激。我花了相当多的时间梳理整个soundcloud文档并且没有真正找到我的答案(它可能在那里,我只是不明白)...

这是关于访问帐户的文档中的一点,它在初始身份验证后应该有令牌,但是再次没有提到访问令牌应该来自何处或者我可以找到它。

SoundCloud docs

# create a client object with access token
client = Soundcloud.new(:access_token => 'YOUR_ACCESS_TOKEN')

# print out the user's username
puts client.get('/me').username

# update the user's profile description
user = client.post('/me', :description => 'I am using the SoundCloud API!')
puts user.description

更新1我已经找到了access_token的来源,...在客户端授予访问权限后,在重定向之后的params中有一个代码,这个:代码被交换为access_token,..使用此代码< / p>

def new
        client = Soundcloud.new(:client_id => "CLIENT_ID",
                        :client_secret => "CLIENT_SECRET",
                        :redirect_uri => 'http://localhost:3000/users/show')

        redirect_to client.authorize_url()

        code = params[:code]
    access_token = client.exchange_token(:code => code)
end

现在我收到一个错误,说“未定义的方法`合并!”为零:NilClass“
这是几个月前在这里发布的另一个人的情况。

Getting 'undefined method `merge!' when merging SoundCloud exchange token

很多讨论,但没有明确的答案......如果有人能够朝着正确的方向推动我,那就太棒了。

值得注意的是,在重定向后的URL中,我看到了:access_token和a:代码,但我不清楚如何让这些参数在我的代码中显示在正确的位置

更新2:我能够共同回答答案,但如果你知道更好的方法,请随时回答。

    def new


        @client = Soundcloud.new(:client_id => "CLIENT_ID",
                        :client_secret => "CLIENT_SECRET",
                        :redirect_uri => 'http://localhost:3000/users/show')

        redirect_to @client.authorize_url(:scope => "non-expiring")

    end 

    def show 
   @client = Soundcloud.new(:client_id => "CLIENT_ID",
                        :client_secret => "CLIENT_SECRET",
                        :redirect_uri => 'http://localhost:3000/users/show')

   @code = params[:code]
   @access_token = @client.exchange_token(:code => @code)  
   @authed_client = Soundcloud.new(:access_token => @access_token)


    end
end

我不完全确定,为什么这样做以及我之前的方式没有,但如果遇到同样的问题你可以试试..

0 个答案:

没有答案