我正在为 python 制作一个 python twitch.tv API Wrapper,到目前为止我有:
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)')
(秘密是一个 API 标识符,密码已被撤回,原因很明显;D)。
服务器应返回如下 JSON 响应:
{
"scope":["user_read"],
"access_token":"df4yofxyn2s7240ojfrh9chz8"
}
程序因异常而崩溃urllib2.HTTPError: HTTP Error 400: Bad Request
。我不知道我做错了什么,是线路分裂还是 urllib2 的使用不正确?我该如何解决?