Created
September 2, 2012 08:32
-
-
Save gnunicorn/3595898 to your computer and use it in GitHub Desktop.
oauth2 authentiaction stream for soundcloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BaseHTTPServer | |
import soundcloud | |
import urlparse | |
""" | |
A simple server for Soundcloud OAuth2 authentication for test cases. | |
Please make sure the callback for your app is set to http://localhost:10101/callback. | |
After you've provided the credentials as follows, open your browser and point it to | |
http://localhost:10101/ - it will immediatly redirect you to soundcloud where you login | |
and grant access to the app. Soundcloud will redirect you to this small webserver here | |
and show you the exchanged access token right after. | |
You can copy-past this token anywhere else. If it breaks, just restart the script and | |
open the browser again to acquire a new access token. | |
Have fun | |
""" | |
CLIENT_ID = "" | |
CLIENT_SECRET = "" | |
REDIR = "http://localhost:10101/callback" | |
client = soundcloud.Client(client_id=CLIENT_ID, | |
client_secret=CLIENT_SECRET, | |
redirect_uri=REDIR) | |
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
def do_GET(self, *args, **kwargs): | |
if (self.path == "/"): | |
self.send_response(302) | |
self.send_header("Location", client.authorize_url()) | |
self.end_headers() | |
return | |
splitted = self.path.split("?") | |
if len(splitted) != 2: | |
self.send_response(401, "Error. Bad Paramters given.") | |
return | |
params = urlparse.parse_qs(splitted[1]) | |
if not "code" in params: | |
self.send_response(401, "Error. No code returned.") | |
return | |
token_resource = client.exchange_token(params['code'][0]) | |
access_token = token_resource.obj['access_token'] | |
self.send_response(200) | |
self.send_header("Content-type", "text/plain") | |
self.end_headers() | |
self.wfile.write("Your access_token is: %s" % access_token) | |
def run(): | |
server_address = ('localhost', 10101) | |
httpd = BaseHTTPServer.HTTPServer(server_address, RequestHandler) | |
print("Please redirect your browser to http://%s:%d/ now" % server_address) | |
httpd.serve_forever() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment