title: trying to implement SoundCloud api having trouble finding access token
body: <p>I have been able to have a user log in with soundcloud, but after they have logged in, I dont have access to their information, bc at this point I believe i need an access_token.  It is unclear to me where I would get this access token.  In my testing im just trying to access my own account</p>
  
  <p>users_controller.rb</p>
  
  <pre><code>class UsersController &lt; ApplicationController 
  require 'soundcloud'
      def index
  
      end
  
  
      def new
          client = Soundcloud.new(:client_id =&gt; "CLIENT_ID",
                          :client_secret =&gt; "CLIENT_SECRET",
                          :scope =&gt; "non-expiring",
                          :redirect_uri =&gt; 'http://localhost:3000/users/show')
  
          redirect_to client.authorize_url()
      end 
  
      def show 
          client = Soundcloud.new(:access_token =&gt; 'ACCESS_TOKEN_SHOULD_GO_HERE')
          puts client.get('/me').username
      end
  end
  </code></pre>
  
  <p>not sure what other information I should provide the app is pretty thin, but any help would be greatly appreciated.  I have spent quite a bit of time combing through the soundcloud docs and have not really found my answer there ( its possible maybe its there and i just didnt understand it)...</p>
  
  <p>This is the bit from the docs where it talks about accessing an account, it sats I should have the token after initial authentication, but again no mention of where the access token should come from or where I can find it.</p>
  
  <p>SoundCloud docs</p>
  
  <pre><code># create a client object with access token
  client = Soundcloud.new(:access_token =&gt; '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 =&gt; 'I am using the SoundCloud API!')
  puts user.description
  </code></pre>
  
  <p>UPDATE 1 I have figured out where the access_token comes from, .. after the client grants access, then there is a code in the params after the redirect  and this :code is exchanged for an access_token, ..using this code</p>
  
  <pre><code>def new
          client = Soundcloud.new(:client_id =&gt; "CLIENT_ID",
                          :client_secret =&gt; "CLIENT_SECRET",
                          :redirect_uri =&gt; 'http://localhost:3000/users/show')
  
          redirect_to client.authorize_url()
  
          code = params[:code]
      access_token = client.exchange_token(:code =&gt; code)
  end
  </code></pre>
  
  <p>now I am getting an error saying "undefined method `merge!' for nil:NilClass"<br>
  this is the same scenario another guy posted about a few months ago here..</p>
  
  <p><a href="https://stackoverflow.com/questions/23646602/getting-undefined-method-merge-when-merging-soundcloud-exchange-token">Getting &#39;undefined method `merge!&#39; when merging SoundCloud exchange token</a></p>
  
  <p>lots of discussion, but no clear answer.. would be great if someone could nudge me in the right direction.</p>
  
  <p>its worth noting that in the URL after the redirect, I see an :access_token and also a :code, but I am unclear how to get those params to show up in my code in the correct places</p>
  
  <p>UPDATE 2:  I was able to hack together an answer, but feel free to answer if you know of a better way.</p>
  
  <pre><code>    def new
  
  
          @client = Soundcloud.new(:client_id =&gt; "CLIENT_ID",
                          :client_secret =&gt; "CLIENT_SECRET",
                          :redirect_uri =&gt; 'http://localhost:3000/users/show')
  
          redirect_to @client.authorize_url(:scope =&gt; "non-expiring")
  
      end 
  
      def show 
     @client = Soundcloud.new(:client_id =&gt; "CLIENT_ID",
                          :client_secret =&gt; "CLIENT_SECRET",
                          :redirect_uri =&gt; 'http://localhost:3000/users/show')
  
     @code = params[:code]
     @access_token = @client.exchange_token(:code =&gt; @code)  
     @authed_client = Soundcloud.new(:access_token =&gt; @access_token)
  
  
      end
  end
  </code></pre>
  
  <p>I am not totally sure, why this works and the way I had it earlier didnt, but in case you run into the same problem you can try it..</p>