Search code examples
phpapiauthenticationoauth-2.0soundcloud

Automatic Soundcloud PHP Api authentication without user interaction


In my application i want to use the Soundcloud API with my own Soundcloud user. The Soundcloud API authentication process involves a user being redirected to the Soundcloud homepage, login and authorize the application, so that the page can use the API for this user.

I want to automate the whole process, because my own user is the only user which gets authenticated. Is that possible?

Here is my code so far:

    $soundcloud = new \Services_Soundcloud(
        '**',
        '**',
        'http://**'
    );

    $authorizeUrl = $soundcloud->getAuthorizeUrl();

    $accessToken = $soundcloud->accessToken();

    try {
        $me = json_decode($soundcloud->get('me'), true);
    } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
        exit($e->getMessage());
    }

But the line $accessToken = $soundcloud->accessToken(); throws an exception:

The requested URL responded with HTTP code 401.
500 Internal Server Error - Services_Soundcloud_Invalid_Http_Response_Code_Exception 

Solution

  • I'm looking for the same thing, but according to the soundcloud's api (check the Authenticating without the SoundCloud Connect Screen paragraph):

    // this example is not supported by the PHP SDK
    

    ..and is not supported by the Javascript neither.

    I've tryed to auth with python:

    # create client object with app and user credentials
    client = soundcloud.Client(client_id='YOUR_CLIENT_ID',
                               client_secret='YOUR_CLIENT_SECRET',
                               username='YOUR_USERNAME',
                               password='YOUR_PASSWORD')
    

    ..then the uploading python method:

    # upload audio file
    track = client.post('/tracks', track={
        'title': 'This is my sound',
        'asset_data': open('file.mp3', 'rb')
    })
    

    and it works just fine.

    So, for now, you have 2 ways:

    1. Use another language, Python or Ruby (the only 2 sdk that actually support this feature) or use a small python/ruby script as a bridge for this particular need;
    2. Add this funcionaliy to the PHP SDK (i'm trying to do it quick'n'dirty, if i get success, i'll share ;)