How to upload files to Soundcloud using Python?
Question: I am building an application that would record what people say, generate an
audio file and upload it to SoundCloud and get the URL of the uploaded track
using Python.

I used PyAudio to record and generate an audio file - a wave file.

But I need to know how to upload the file to SoundCloud. By research I found
there is a Python wrapper for SoundCloud API and with Python library Poster,
one can easily upload files to SoundCloud.

How do I do it? I have not used this API thing before and I don't find a
proper tutorial or a guide to how to make use of it. So if anybody can help me
with this, please answer my question here.

How to use this [SoundCloud Python API
wrapper](https://github.com/soundcloud/python-api-wrapper) to upload files to
SoundCloud using Python with the help of the Python library
[Poster](http://atlee.ca/software/poster/)?


Answer: We just released a new Python API wrapper. You can get it on
[PyPi](http://pypi.python.org/pypi/soundcloud/) or from our [Github
account](https://github.com/soundcloud/soundcloud-python). To upload a track,
you'll want to first get an access token using one of the supported OAuth2
auth flows. You can read about that in the [README
file](https://github.com/soundcloud/soundcloud-python/blob/master/README.rst).
Let me know if you want me to elaborate on auth and I can edit my answer.

To get an access token, first [register your application on
soundcloud.com](http://soundcloud.com/you/apps). You will need to provide a
URI that users will be directed to after authorizing your application and you
will be given a client id and client secret. Once you have those credentials,
pass them to the Client constructor:

    
    
    import soundcloud
    client = soundcloud.Client(client_id=YOUR_CLIENT_ID,
                               client_secret=YOUR_CLIENT_SECRET,
                               redirect_uri="http://your/redirect/uri")
    

You'll then be able to redirect the user to the authorization URL in order to
authorize your app. The user will be sent to soundcloud.com to log in (if they
do not have an active session) and approve access for your app. Depending on
the framework you're using (e.g. Django, Flask, etc) it could look something
like this:

    
    
    return redirect(client.authorize_url)
    

After approving access for your app, the user will be redirected to the
redirect uri you specified when registering your app and in the constructor.
The URL will have a query string that includes a 'code' parameter which you
can then use to obtain an access token. Again, depending on the framework
you're using, this could look like this:

    
    
    code = request.params.get('code')
    token = client.exchange_token(code)
    print token.access_token  # don't actually print it, just showing how you would access it
    

You should probably store the access token (i.e. in some kind of data store
like MySQL or Redis) so you can use it whenever that user wants to access
SoundCloud in the future.

Once you've got an access token, uploading a track should be pretty simple.
Once you've got your audio file, just send a POST request to the tracks
resource. Here's an example:

    
    
    import soundcloud
    
    client = soundcloud.Client(access_token='YOUR_ACCESS_TOKEN')
    track = client.post('/tracks', track={
        'title': 'The title you want to give your track',
        'sharing': 'private',  # make this 'public' if you want
        'asset_data': open('yourtrack.mp4', 'rb')
    })
    

You'll get back a track resource, which you can then use to get the Soundcloud
URL:

    
    
    print track.permalink_url
    

Hope that helps! Let me know if you have any questions.

