Welcome Guest, Not a member yet? Register   Sign In
REST Client + SoundCloud
#1

[eluser]austintbiggs[/eluser]
I'm trying to utilize the SoundCloud API using Phil Sturgeon's Rest Client which sits on the curl spark for those that haven't played with it. I'm currently able to load user information through the API using my Client ID, but as far as auth tokens I'm totally list on how to create or refresh them.

I've taken a look at Ben Edmunds' Soundcloud library to try to grasp the concept, but the library uses OAuth1 instead of OAuth2 which SoundCloud is now pushing for everyone to use.

SoundCloud Docs

As I understand it my app needs to create a link

Code:
<a href="&lt;?php echo $server.$authorize_url.'?client_id='.$client_id.'&response;_type='.$response_type.'&redirect;_uri='.$redirect_url;?&gt;">Connect</a>

and if the SoundCloud user authorizes my app then the user is redirected back to my app, while the url contains a query string

Quote:http://yourapp.com/soundcloud/oauth-call...WsdhqVQr3g

Then I need to be able to extract the "code" parameter of the url and use it in a curl call to

Quote:https://api.soundcloud.com/oauth2/token

Example Curl
Code:
$ curl "https://api.soundcloud.com/oauth2/token"
           -d 'client_id=YOUR_CLIENT_ID'
           -d 'client_secret=YOUR_CLIENT_SECRET'
           -d 'grant_type=authorization_code'
           -d 'redirect_uri=http://yourapp.com/soundcloud/oauth-callback'
           -d 'code=0000000EYAA1CRGodSoKJ9WsdhqVQr3g'

Example returned from Curl
Code:
{
  "access_token": "04u7h-4cc355-70k3n",
  "expires_in": 3600,
  "scope": null,
  "refresh_token": "04u7h-r3fr35h-70k3n"
}

Problems I'm facing:
1. How to get the "code" parameter from the returned url.
2. How to make the exchange for the tokens
3. What do I do with the token? Store it in a variable? A MySQL database? Other?
4. Is there a better way to load and utilize the API data? [see code below]

My example controller : api.php
Code:
&lt;?php
class Api extends CI_Controller {

  function __construct()
    {
        parent::__construct();
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->database();
        $this->load->helper('url');
        
        $client_id = $this->config->item('client_id');
      $client_secret = $this->config->item('client_secret');
      $redirect_url = $this->config->item('redirect_url');
      $authorize_url = $this->config->item('authorize_url');
      $token_url = $this->config->item('token_url');
    $response_type = $this->config->item('response_type');
        
        $this->data['user'] = $this->ion_auth->get_user();
        
        $this->load->spark('restclient/2.0.0');
    }

    public function index()
    {
    $client_id = $this->config->item('client_id');
    
            // Run some setup
        $this->rest->initialize(array('server' => 'http://api.soundcloud.com/'));
    
        $this->data['soundcloud'] = $this->rest->get('users/3207.json?client_id=EqtzDhIRdXmqhRsEMFKiGw');
    
    
      $this->data['title'] = "API";
      $this->data['main_content'] = "api/index";
        $this->load->view('body/blank', $this->data);
        

        
        
        
    }
  }

My example view : index.php
Code:
&lt;?php echo $soundcloud->id;?&gt;

As you can see I haven't made it very far as I'm stuck with the OAuth2 dance, any and all help is appreciated.

Austin.




Theme © iAndrew 2016 - Forum software by © MyBB