PyMel - 从函数调用时,urllib2 400 Bad Request

时间:2017-03-30 00:35:24

标签: urllib2 maya pymel

如果标题太模糊,我道歉。

在Maya(PyMel)中我调用下面的函数来绘制登录屏幕

def ms_ui(self):
    logged_in = False
    if not logged_in:
        login_col = pm.rowColumnLayout(numberOfColumns=3)
        usr = pm.textField(text="username")
        usr_val = pm.textField(usr, text=True, q=True)
        pwd = pm.textField(text="Password")
        pwd_val = pm.textField(pwd, text=True, q=True)
        pm.button(label="Log in!", c=lambda x: ms.ms_login(usr_val, pwd_val))

单击登录按钮然后调用ms.ms_login并将用户名和密码的值传递给函数

def ms_login(user, passwd):
import urllib2, base64, json
username = user
password = passwd
param = {"secret": "thesecretgoeshere"}
url = "https://someurl/api/token"
base64string = base64.b64encode('%s:%s' % (username, password))
req = urllib2.Request(url,
    headers = {
        "Authorization": "Basic %s" % base64string,
        "Content-Type": "application/json"
        })
f = urllib2.urlopen(req, json.dumps(param))
data = f.read()
print data

但是我收到以下错误

    Error: HTTP Error 400: Bad Request
Traceback (most recent call last):
File "C:\Program Files\Autodesk\Maya2017\Python\lib\site-packages\pymel\internal\factories.py", line 785, in callback
     res = origCallback(*newargs)
   File "D:/Documents/maya/2017/scripts\MMB\MMB.py", line 223, in <lambda>
     pm.button(label="Log in!", c=lambda x: ms.ms_login(usr_val, pwd_val))
   File "D:/Documents/maya/2017/scripts\MMB\ms.py", line 13, in ms_login
     f = urllib2.urlopen(req, json.dumps(param))
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 154, in urlopen
     return opener.open(url, data, timeout)
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 437, in open
     response = meth(req, response)
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 550, in http_response
     'http', request, response, code, msg, hdrs)
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 475, in error
     return self._call_chain(*args)
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 409, in _call_chain
     result = func(*args)
   File "C:\Program Files\Autodesk\Maya2017\bin\python27.zip\urllib2.py", line 558, in http_error_default
     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
 HTTPError: HTTP Error 400: Bad Request # 

现在,我知道400个不良请求是什么,但我不明白为什么我得到一个。在maya控制台中自己执行该函数可以得到我想要的结果,它似乎只是在用lambda或partial调用函数时。

搜索时无法找到解决方案(我道歉,之前从未真正使用过urllib)

任何建议都将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

请尝试以下版本:

import pymel.core as pm
import urllib2, base64, json

window = pm.window( title="Login Window", iconName='Login Window', widthHeight=(320,30) )
login_col = pm.rowColumnLayout( numberOfColumns=3 )
usr = pm.textField( text="username" )
usr_val = pm.textField( usr, text=True, q=True )
pwd = pm.textField( text="password" )
pwd_val = pm.textField( pwd, text=True, q=True )
pm.button( label="Log in!", command=('pm.deleteUI(\"' + window + '\", window=True)') )
pm.setParent( '..' )
pm.showWindow( window )

def ms_ui(self):

    logged_in = False
    if not logged_in:
        pm.button( c=lambda x: ms.ms_login(usr_val, pwd_val) )       

def ms_login(user, passwd):

    username = user
    password = passwd
    param = {"secret": "thesecretgoeshere"}
    url = "https://someurl/api/token"
    base64string = base64.b64encode('%s:%s' % (username, password))
    req = urllib2.Request(url,
        headers = {
            "Authorization": "Basic %s" % base64string,
            "Content-Type": "application/json"
        })
    f = urllib2.urlopen( req, json.dumps(param) )
    data = f.read()
    print data

enter image description here