1-
21import tornado .httpserver
32import tornado .ioloop
43import tornado .web
1312
1413
1514class MainHandler (tornado .web .RequestHandler ):
16- def initialize (self , config , output ):
17- self .config = dueros .config .load (configfile = config )
15+ def initialize (self , output ):
16+ self .config = dueros .config .load ()
1817 self .output = output
1918
20- if ('host_url' in self .config ) and self .config ['host_url' ] == 'dueros-h2.baidu.com' :
21- self .token_url = 'https://openapi.baidu.com/oauth/2.0/token'
22- self .oauth_url = 'https://openapi.baidu.com/oauth/2.0/authorize'
23- self .scope = 'basic'
19+ self .token_url = 'https://openapi.baidu.com/oauth/2.0/token'
20+ self .oauth_url = 'https://openapi.baidu.com/oauth/2.0/authorize'
2421
2522 @tornado .web .asynchronous
2623 def get (self ):
2724 redirect_uri = self .request .protocol + "://" + self .request .host + "/authresponse"
28- print '================request.path=' ,self .request .path
25+ print '================request.path=' , self .request .path
2926 if self .request .path == '/authresponse' :
3027 code = self .get_argument ("code" )
3128 payload = {
@@ -56,40 +53,34 @@ def get(self):
5653 else :
5754 payload = {
5855 "client_id" : self .config ['client_id' ],
59- "scope" : self .scope ,
60- # "scope_data": scope_data,
56+ "scope" : 'basic' ,
6157 "response_type" : "code" ,
6258 "redirect_uri" : redirect_uri
6359 }
6460
65-
6661 req = requests .Request ('GET' , self .oauth_url , params = payload )
6762 p = req .prepare ()
68- print '============redirect url=' ,p .url
63+ print '============redirect url=' , p .url
6964 self .redirect (p .url )
7065
7166
72- def login (config , output ):
73- application = tornado .web .Application ([(r".*" , MainHandler , dict (config = config , output = output ))])
67+ def login ():
68+ application = tornado .web .Application ([(r".*" , MainHandler , dict (output = dueros . config . DEFAULT_CONFIG_FILE ))])
7469 http_server = tornado .httpserver .HTTPServer (application )
7570 http_server .listen (3000 )
7671 tornado .ioloop .IOLoop .instance ().start ()
7772 tornado .ioloop .IOLoop .instance ().close ()
7873
79-
80- @click .command ()
81- @click .option ('--config' , '-c' , help = 'configuration json file with product_id, client_id and client_secret' )
82- @click .option ('--output' , '-o' , default = dueros .config .DEFAULT_CONFIG_FILE , help = 'output json file with refresh token' )
83- def main (config , output ):
74+ def main ():
8475 try :
8576 import webbrowser
8677 except ImportError :
8778 print ('Go to http://{your device IP}:3000 to start' )
88- login (config , output )
79+ login ()
8980 return
9081
9182 import threading
92- webserver = threading .Thread (target = login , args = ( config , output ) )
83+ webserver = threading .Thread (target = login )
9384 webserver .daemon = True
9485 webserver .start ()
9586 print ("A web page should is opened. If not, go to http://127.0.0.1:3000 to start" )
@@ -104,4 +95,3 @@ def main(config, output):
10495
10596if __name__ == '__main__' :
10697 main ()
107-
0 commit comments