0

I am making a python twitch.tv API Wrapper for python and so far I have :

import urllib2
import json
import time


waittime = 1
baseurl = 'https://api.twitch.tv/kraken/'
secret = '(CLASSIFIED)'


class twitchchannelinfo(): #Ignore me ;D I'm Here for reference's sake
    def __init__ (self,channel):
        self.channel = channel
        time.sleep(waittime)
        self.dict1 = json.loads(urllib2.urlopen(baseurl + 'channels/' + channel).read())

    def getstatus(self):
        return self.dict1 ['status']
    def getdisplay_name(self):
        return self.dict1 ['display_name']
    def getmature(self):
        return self.dict1 ['mature']
    def getchanurl(self):
        return self.dict1 ['url']
    def getcreated_at(self):
        return self.dict1 ['created_at']
    def getteams(self):
        return self.dict1 ['teams']
    def getgame(self):
        return self.dict1 ['game']
    def getupdated_at(self):
        return self.dict1 ['updated_at']

class twichtvwrapper():
    def __init__(self,username,password):        
        self.username = username
        self.password = password
        self.dict1 = json.loads(urllib2.urlopen(baseurl + 'oauth2/token',\
                        "client_id=<3kfp6al05voejvv7ofmpc94g4jga0tb>&\
                        client_secret=<" + secret + ">&\
                        username=<" + self.username + ">&\
                        password=<" + self.password + ">&\
                        scope=user_read&\
                        grant_type=password"))
        print self.dict1

me = twichtvwrapper('martincharles07','(CLASSIFIED)')

(The secret is an API identifier and the password has been retracted for obvious reasons ;D).

The server should return a JSON response like this:

{
    "scope":["user_read"],
    "access_token":"df4yofxyn2s7240ojfrh9chz8"
}

Here is the API reference.

The program crashes with the exception urllib2.HTTPError: HTTP Error 400: Bad Request. I don't know what I am doing wrong, is it the line splits or incorrect usage of urllib2? How can I fix it?

  • 1
    Have you ever tried [Requests](http://docs.python-requests.org/en/latest/). It's easier and pretty well documented too. – xbb Mar 13 '13 at 15:51

1 Answers1

0

Instead of concatenating your string together try something like

body={'username':'someusername','password':'somepassword', ...}
txdata = urllib.urlencode(body)

You'll have to use urllib instead of urllib2 to use that method.

Here's an untested snippet.

import json
import urllib
import urllib2

def wrapper(user, password, secret):

    data = {"client_id":"<3kfp6al05voejvv7ofmpc94g4jga0tb>",
            "client_secret":secret,
            "username":username,
            "password":password,
            "scope":"user_read",
            "grant_type":"password",
            }

    txdata = urllib.urlencode(data)
    response = urllib2.urlopen(baseurl + 'oauth2/token', txdata)
    return_data = json.loads(response.read())

    return return_data
John
  • 13,197
  • 7
  • 51
  • 101
  • Is there a way I can do this with urllib2? –  Mar 13 '13 at 15:39
  • @user76255 Yes you can keep using urllib2. Just `from urllib import urlencode` and then use urlencode. Also as mentioned in the comments the Requests module is pretty good. For additional info see [this answer](http://stackoverflow.com/a/2018074/322909). – John Mar 13 '13 at 16:14