Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 597
Alp Avatar asked Jan 03 '12 06:01

Alp


2 Answers

Hi All,

Here I am going to share my experience with Soundcloud API (PHP)

See my Question: Link

Recently I started to work with Sound cloud API (PHP) and I decided to use PHP API by

https://github.com/mptre/php-soundcloud.

But When I was trying to get access token from Sound cloud server by this code:
// Get access token
try {
    $accessToken = $soundcloud->accessToken($_GET['code']);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

I had check the $_GET['code'] value. But strange there is nothing in $_GET['code'] this is blank. The Soundcloud was returning "The requested URL responded with HTTP code 0" error. That time I was testing Soundcloud on WAMP Localhost.

Allot of Goggling I found a solution to fix "The requested URL responded with HTTP code 0" issue. I had download 'cacert.pem' file and put inside our demo project folder (inside Services/Soundcloud/). Then after I added some code in 'class Services_Soundcloud'

function protected function _request($url, $curlOptions = array()).
// My code in side function
$curlPath = realpath(getcwd().'\Services\cacert.pem');         
$curlSSLSertificate = str_replace("\\", DIRECTORY_SEPARATOR, $curlPath);        
curl_setopt($ch, CURLOPT_CAINFO, $curlSSLSertificate);

Saved 'class Services_Soundcloud' file and moved on live server. After move my project from WAMP to Live server I start to check it again. When I open my index.php it's ask me to login

enter image description here

I use my Facebook account to login.

enter image description here

after login it was asking to connect with Soundcloud

enter image description here

after connect everything working smooth, I got my info with

$me = json_decode($soundcloud->get('me'));

but a new problem start to occurring which was that my access token being expire again and again. Then I use session :D

// code for access token
$code = $_GET['code'];
// Get access token
try {
    if(!isset($_SESSION['token'])){
        $accessToken = $soundcloud->accessToken($code);
        $_SESSION['token'] = $accessToken['access_token'];
    }else{
        $soundcloud->setAccessToken($_SESSION['token']);
    }   
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

And now everything working awesome. i can get all my details, tracks everything from SC server

Hope it will help you to fight with Soundcloud API Cheers!!!! :)

like image 59
Nono Avatar answered Sep 21 '22 13:09

Nono


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 ;)
like image 43
Strae Avatar answered Sep 25 '22 13:09

Strae