Commits

Anonymous committed 6a4a053 Draft

use filmaster.com instead filmaster.pl, do not use _OAuthHandler

Comments (0)

Files changed (1)

filmaster_auth.py

 
     """
 
-    API_BASE_URL = "http://api.filmaster.com"
-    BASE_URL = 'http://filmaster.com'
-    
-    API_BASE_URL = "http://api.filmaster.pl"
-    BASE_URL = 'http://filmaster.pl'
+    API_BASE_URL = "https://api.filmaster.com"
+    BASE_URL = 'https://filmaster.com'
+
+    # API_BASE_URL = "http://api.filmaster.pl"
+    # BASE_URL = 'http://filmaster.pl'
+
+    # API_BASE_URL = "http://localhost.pl:8000/api"
+    # BASE_URL = "http://localhost.pl:8000"
 
     REQUEST_TOKEN_URL = BASE_URL + '/oauth/request/token/'
     ACCESS_TOKEN_URL = BASE_URL + '/oauth/access/token/'
         access_token - optional, may be set later using set_access_token method
         """
         self.consumer = oauth.OAuthConsumer(key, secret)
-        self.signature = oauth.OAuthSignatureMethod_HMAC_SHA1()
-        self._opener = None
-        if access_token:
-            self.set_access_token(access_token)
+        self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
+        # self.signature_method = oauth.OAuthSignatureMethod_PLAINTEXT()
+        self.set_access_token(access_token)
 
     def fetch_request_token(self):
         """
             verifier=verifier,
             http_url=token_url,
         )
-        oauth_request.sign_request(self.signature, self.consumer, request_token)
-        req = _Request(oauth_request.http_method, token_url, headers=oauth_request.to_header())
-        logger.debug("fetching token: %r", req)
-        response=urllib2.urlopen(req).read()
-        logger.debug("response: %s", response)
+        oauth_request.sign_request(self.signature_method, self.consumer, request_token)
+        headers = oauth_request.to_header()
+        response=self.fetchurl(oauth_request.http_method, token_url, None, headers)
         return oauth.OAuthToken.from_string(response)
 
     def set_access_token(self, access_token):
-        self.access_token = self._parse_token(access_token)
-        self._opener = urllib2.build_opener(_OAuthHandler(self.consumer, self.access_token))
+        self.access_token = access_token and self._parse_token(access_token)
 
     def facebook_login(self, fb_access_token):
         request_token = self.fetch_request_token()
         authorize_url = self.get_authorize_fb_url(request_token, fb_access_token)
-        logger.debug('facebook authorization url: %s', authorize_url)
-        response = urllib2.urlopen(authorize_url).read()
-        logger.debug('response: %r', response)
+        response = self.fetchurl('GET', authorize_url)
         verifier = dict(cgi.parse_qsl(response)).get('oauth_verifier')
         return self.fetch_access_token(request_token, verifier)
 
     def do_request(self, method, url, data=None, headers = None):
-        if not self._opener:
+        if not self.access_token:
             raise Exception('access token not set')
 
         if url.startswith('/'):
             headers['Content-Type'] = 'application/json'
             headers['Content-Length'] = str(len(data))
 
+        params = {}
+        if '?' in url:
+            params.update(cgi.parse_qsl(url.split('?',1)[1]))
+
+        content_type = headers.get('Content-Type')
+        if content_type in [None, 'application/x-www-form-urlencoded'] and data:
+            params.update(cgi.parse_qsl(data))
+
+        oauth_request = oauth.OAuthRequest.from_consumer_and_token(
+             self.consumer, 
+             token = self.access_token,
+             http_method = method,
+             http_url = url,
+             parameters = params)
+        oauth_request.sign_request(self.signature_method, self.consumer, token = self.access_token)
+        headers.update(oauth_request.to_header())
+
+        return json.loads(self.fetchurl(method, url, data, headers))
+
+        # test for sending oauth data in url:
+        # return json.loads(self.fetchurl(method, oauth_request.to_url(), data, headers))
+
+    def fetchurl(self, method, url, data=None, headers=None):
         request = _Request(method, url, data, headers)
-        return json.loads(self._opener.open(request).read())
+        logger.debug("%r", request)
+        response = urllib2.urlopen(request).read()
+        logger.debug("response: %r", response)
+        return response
 
     def get(self, url):
         """
              http_method = request.get_method().upper(), 
              http_url = url, 
              parameters = params)
-        oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, token = self.access_token)
+        oauth_request.sign_request(self.signature_method, self.consumer, token = self.access_token)
         oauth_headers = oauth_request.to_header()
         request.headers.update(oauth_headers)
         logger.debug('request: %s', request)