Exemplo n.º 1
0
    def oauth_handshake(self, tokens):
        """Exchange an intermediary access_code for an OAuth2.0 access_token

        tokens - dict - 'app_key' = API_key, 'client_secret' = client secret, 'access_code' = access_code

        Returns: an Oauth2.0 access_token, see http://developer.eventbrite.com/doc/authentication/oauth2/
        """
        params = {
            'grant_type': 'authorization_code',
            'client_id': tokens['app_key'],
            'client_secret': tokens['client_secret'],
            'code': tokens['access_code']
        }

        request_url = 'https://%(host)s/oauth/token' % dict(
            host=self.eventbrite_api_endpoint)
        post_body = urllib.urlencode(params)
        headers = {'Content-type': "application/x-www-form-urlencoded"}

        self._https_connection.request('POST', request_url, post_body, headers)
        response_data = self._https_connection.getresponse().read()
        response = json_lib.loads(response_data)

        if 'error' in response or 'access_token' not in response:
            raise EnvironmentError(response['error_description'])

        return response
Exemplo n.º 2
0
    def _request(self, method='', params=dict()):
        """Execute an API call on Eventbrite using their HTTP-based API

        method - string  - the API method to call - see https://www.eventbrite.com/doc
        params - dict    - Arguments to pass along as method request parameters

        Returns: A dictionary with a return structure defined at http://developer.eventbrite.com/doc/
        """
        #unpack our params
        if type(params) == type(()) and len(params) > 0:
            method_arguments = dict(params[0])
        else:
            method_arguments = {}

        # Add authentication tokens
        if 'access_token' not in self._auth_tokens:
            method_arguments.update(self._auth_tokens)

        # urlencode API method parameters
        encoded_params = urllib.urlencode(method_arguments)

        # construct our request url
        request_url = self.eventbrite_request_template % dict(
            host=self.eventbrite_api_endpoint,
            method=method,
            arguments=encoded_params)
        #self.logger.debug("REQ - %s", request_url)

        # Send a GET request to Eventbrite
        # if using OAuth2.0 for authentication, set additional headers
        if 'access_token' in self._auth_tokens:
            self._https_connection.request('GET', request_url, None, {
                'Authorization':
                "Bearer " + self._auth_tokens['access_token']
            })
        else:
            self._https_connection.request('GET', request_url)

        # extending the timeout window per request (not system-wide)
        # requires python 2.5 or better:
        if int(sys.version[0]) >= 2 and int(sys.version[2]) >= 5:
            self._https_connection.sock.settimeout(200)

        # Read the JSON response
        response_data = self._https_connection.getresponse().read()
        #self.logger.debug("RES - %s", response_data)

        # decode our response
        response = json_lib.loads(response_data)
        if 'error' in response and 'error_message' in response['error']:
            raise EnvironmentError(response['error']['error_message'])
        return response
    def _request(self, method='', params=dict()):
        """Execute an API call on Eventbrite using their HTTP-based API

        method - string  - the API method to call - see https://www.eventbrite.com/doc
        params - dict    - Arguments to pass along as method request parameters

        Returns: A dictionary with a return structure defined at http://developer.eventbrite.com/doc/
        """
        #unpack our params
        if type(params) == type(()) and len(params) > 0: 
            method_arguments = dict(params[0])
        else:
            method_arguments = {}

        # Add authentication tokens
        if 'access_token' not in self._auth_tokens:
            method_arguments.update(self._auth_tokens)

        # urlencode API method parameters
        encoded_params = urllib.urlencode(method_arguments)
        
        # construct our request url
        request_url = self.eventbrite_request_template % dict(host=self.eventbrite_api_endpoint, method=method, arguments=encoded_params)
        #self.logger.debug("REQ - %s", request_url)

        # Send a GET request to Eventbrite
        # if using OAuth2.0 for authentication, set additional headers
        if 'access_token' in self._auth_tokens:
            self._https_connection.request('GET', request_url, None, {'Authorization': "Bearer " + self._auth_tokens['access_token']})
        else:
            self._https_connection.request('GET', request_url)

        # extending the timeout window per request (not system-wide) 
        # requires python 2.5 or better:
        if int(sys.version[0]) >= 2 and int(sys.version[2]) >= 5:
            self._https_connection.sock.settimeout(200)

        # Read the JSON response 
        response_data = self._https_connection.getresponse().read()
        #self.logger.debug("RES - %s", response_data)

        # decode our response
        response = json_lib.loads(response_data)
        if 'error' in response and 'error_message' in response['error'] :
            raise EnvironmentError( response['error']['error_message'] )
        return response
    def oauth_handshake( self, tokens ):
        """Exchange an intermediary access_code for an OAuth2.0 access_token

        tokens - dict - 'app_key' = API_key, 'client_secret' = client secret, 'access_code' = access_code

        Returns: an Oauth2.0 access_token, see http://developer.eventbrite.com/doc/authentication/oauth2/
        """
        params = {'grant_type'   : 'authorization_code', 
                  'client_id'    : tokens['app_key'], 
                  'client_secret': tokens['client_secret'], 
                  'code'         : tokens['access_code'] }

        request_url = 'https://%(host)s/oauth/token' % dict(host=self.eventbrite_api_endpoint)
        post_body = urllib.urlencode(params)
        headers = {'Content-type': "application/x-www-form-urlencoded"}
        
        self._https_connection.request('POST', request_url, post_body, headers)
        response_data = self._https_connection.getresponse().read()
        response = json_lib.loads(response_data)

        if 'error' in response or 'access_token' not in response :
            raise EnvironmentError( response['error_description'] )

        return response