Automatic Soundcloud PHP Api authentication without user interaction

后端 未结 3 959
遇见更好的自我
遇见更好的自我 2020-12-19 11:49

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

相关标签:
3条回答
  • 2020-12-19 12:36

    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 ;)
    0 讨论(0)
  • 2020-12-19 12:44

    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!!!! :)

    0 讨论(0)
  • 2020-12-19 12:48

    There is no magic behind its implementation in Python and Ruby SDK's. What's happening is that POST request is sent to http://api.soundcloud.com/oauth2/token with the following params:

       client_id='YOUR_CLIENT_ID'
       client_secret='YOUR_CLIENT_SECRET'
       username='YOUR_USERNAME'
       password='YOUR_PASSWORD'
    

    And Content-Type: application/x-www-form-urlencoded

    The response body contains access_token, that can be used for the further authorization of your requests. Thus, your GET request to /me endpoint will look like: /me?oauth_token=YOUR_ACCESS_TOKEN&client_id=YOUR_CLIENT_ID. (I believe, client_id is redundant here but all their apps keep adding it).

    Here is the Postman Doc I created for demonstration: https://documenter.getpostman.com/view/3651572/soundcloud/7TT5oD9

    0 讨论(0)
提交回复
热议问题