Oauth 1

mptre edited this page May 9, 2012 · 1 revision
Clone this wiki locally

h1. Getting started

This is a basic example of how to implement the SoundCloud API wrapper in your PHP application. Remember this is just a quick one to get you started. All kind of error handling and validation of user generated data has been stripped out for sake of simplicity.

h3. A few words on running the demo

The included demo in the repository can easily be setup on almost any machine. Just make sure your maximum upload size is set to a appropriate value. You can either do this by editing your php.ini or htaccess file. See example below.

# php.ini
upload_max_filesize = 10M

# htaccess
php_value upload_max_filesize 10M

You also have to make sure the upload directory is writable by the web server. See example below.

$ chmod 777 upload-directory

When your done with the above steps it's time to start doing the OAuth dance.

h3. 1. Register your application

Head over to "SoundCloud":http://soundcloud.com/settings/applications/new and register your new OAuth application in order to maintain your consumer key and secret. These two strings will identify your application. The keys could either be stored as constants, variables or using a database. Whatever suits you best. Also do remember to store your callback url since we'll need it further on.

h3. 2. Download the PHP wrapper

Simply download the wrapper using git (see example below). If you're not familiar with git you could download the whole library directly from GitHub and place the files in your desired location.

$ git clone git://github.com/mptre/php-soundcloud.git

h3. 3. Create a new Soundcloud object

Make sure both the generic OAuth and Soundcloud class are included in your application. Then initialize the Soundcloud class. The constructor takes for parameters. At this step you'll only need or consumer key and secret.

$soundcloud = new Soundcloud('consumer-key', 'consumer-secret');

h3. 4. Authentication Step 1: Get a Request Token from SoundCloud

This is the first step of the authentication. Send a request to SoundCloud asking for request tokens. Make sure to include your callback url (added in OAuth 1.0a). The function namned get_request_token will return an associative array including request token and secret.

$token = $soundcloud->get_request_token('callback-url');

h3. 5. Authentication Step 2: Authorize the Request Token

Ask the user to grant permissions to his/her SoundCloud account. This allows your application access the given account. Note that we use the request token from previous step.

echo $soundcloud->get_authorize_url($token['oauth_token']);

h3. 6. Authentication Step 3: Authorize the Request Token

If all went well the user will be redirected to your predefined callback url. Note that a parameter namned oauth_verifier will be included in the query string. You'll need this one when requesting access tokens.

Overwrite the Soundcloud object with your new request tokens. Then ask for access tokens using the get_access_token function where you'll need to include the oauth_verifier.


Now with your access tokens you can create a fully authorized Soundcloud object. This is where the fun stuff begin.

h3. 7. Retrieve user account details

With your fully authorized Soundcloud object you can start sending real requests to Soundcloud. In the example below you'll ask for the authenticated user's account details.

$me = $soundcloud->request('me');

For further reference on how to get, delete, put and post data see the code for demo included in this repository.