def access_api_resource(self, relative_url, access_token, method="GET"): full_url = self.server_url + relative_url # Escape each parameter value. url = urlparse.urlparse(full_url) query_params = cgi.parse_qs(url.query) for key in query_params: query_params[key] = query_params[key][0] oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=access_token, http_url=full_url, parameters=query_params, http_method=method) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, access_token) file = None ret = None try: if method == "GET": file = urllib2.urlopen(oauth_request.to_url()) else: file = urllib2.urlopen(url.path, oauth_request.to_postdata()) except urllib2.HTTPError, error: # We don't want to treat HTTP error codes (401, 404, etc.) like # exceptional scenarios. We want to pass them along like anything # else. # Luckily, the exception raised here acts very much like an # `HTTPResponse` object. Good enough for our purposes. file = error
def access_resource(self, relative_url, access_token, method="GET", params=None): full_url = self.server_url + relative_url url = urlparse.urlparse(full_url) full_params = cgi.parse_qs(url.query) for key in full_params: full_params[key] = full_params[key][0] if params: full_params.update(params) oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=access_token, http_url=full_url, parameters=full_params, http_method=method) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, access_token) if method == "GET": response = get_response(oauth_request.to_url()) else: response = post_response(full_url, oauth_request.to_postdata()) return response
def start_fetch_request_token(self): oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, http_url="%s/api/auth/request_token" % self.server_url) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) webbrowser.open(oauth_request.to_url())
def _send_request(self, request, token=None): request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, token) conn = self._get_conn() if request.http_method == 'POST': conn.request('POST', request.http_url, body=request.to_postdata()) else: conn.request('GET', request.http_url, headers=request.to_header()) return conn.getresponse()
def start_fetch_request_token(self, callback=None): oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, callback=callback, http_url="%s/api/auth/request_token" % self.server_url) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) return oauth_request.to_url()
def start_fetch_request_token(self, callback=None): oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, callback=callback, http_url="%s/api/auth/request_token" % self.server_url ) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) print 'opening webbrowser with', oauth_request.to_url() webbrowser.open(oauth_request.to_url())
def url_for_request_token(self, callback=None, parameters=None): http_url = "%s/api/auth/request_token" % self.server_url oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, http_url=http_url, callback=callback, parameters=parameters) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) return oauth_request.to_url()
def fetch_access_token(self, request_token): oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=request_token, http_url="%s/api/auth/access_token" % self.server_url) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, request_token) response = get_response(oauth_request.to_url()) return OAuthToken.from_string(response)
def post_resources(self, relative_url, access_token, method="POST", data=None, content_type=None): full_url = self.server_url + relative_url oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=access_token, http_url=full_url, http_method=method) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, access_token) return post_response(oauth_request.to_url(), data, content_type)
def access_resource(self, relative_url, access_token): full_url = self.server_url + relative_url url = urlparse.urlparse(full_url) query_params = cgi.parse_qs(url.query) for key in query_params: query_params[key] = query_params[key][0] oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=access_token, http_url=full_url, parameters=query_params) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, access_token) response = get_response(oauth_request.to_url()) return response.strip()
def url_for_access_token(self, request_token, callback=None, parameters=None, verifier=None): http_url = "%s/api/auth/access_token" % self.server_url if not verifier and request_token.verifier: verifier = request_token.verifier oauth_request = OAuthRequest.from_consumer_and_token( self.consumer, token=request_token, http_url=http_url, callback=callback, parameters=parameters, verifier=verifier) oauth_request.sign_request(OAuthSignatureMethod_HMAC_SHA1(), self.consumer, request_token) return oauth_request.to_url()