示例#1
0
    def _oauth_request_parameters(self, url, access_token, parameters={},
                                  method="GET"):
        """Returns the OAuth parameters as a dict for the given request.

        parameters should include all POST arguments and query string arguments
        that will be sent with the request.
        """
        consumer_token = self._oauth_consumer_token()
        base_args = dict(
            oauth_consumer_key=escape.to_basestring(consumer_token["key"]),
            oauth_token=escape.to_basestring(access_token["key"]),
            oauth_signature_method="HMAC-SHA1",
            oauth_timestamp=str(int(time.time())),
            oauth_nonce=escape.to_basestring(binascii.b2a_hex(uuid.uuid4().bytes)),
            oauth_version="1.0",
        )
        args = {}
        args.update(base_args)
        args.update(parameters)
        if getattr(self, "_OAUTH_VERSION", "1.0a") == "1.0a":
            signature = _oauth10a_signature(consumer_token, method, url, args,
                                            access_token)
        else:
            signature = _oauth_signature(consumer_token, method, url, args,
                                         access_token)
        base_args["oauth_signature"] = signature
        return base_args
示例#2
0
def do_api_request(path, method, post_data={}, body=''):
  try:
    url = urljoin('https://%s.hackpad.com/api/1.0/' % settings.get('hackpad_domain'), path)
    args = dict(
      oauth_consumer_key=escape.to_basestring(settings.get('hackpad_oauth_client_id')),
      oauth_signature_method='HMAC-SHA1',
      oauth_timestamp=str(int(time.time())),
      oauth_nonce=escape.to_basestring(binascii.b2a_hex(uuid.uuid4().bytes)),
      oauth_version='1.0a',
    )
    signature = _oauth10a_signature(
      {
        'key':settings.get('hackpad_oauth_client_id'),
        'secret':settings.get('hackpad_oauth_secret')
      },
      method,
      url,
      args
    )
    args['oauth_signature'] = signature
    api_link = url + '?' + urllib.urlencode(args)
    logging.info(api_link)

    hackpad = {}
    if method.lower() == 'post':
      r = requests.post(
        api_link,
        data=body,
        headers={'Content-Type': 'text/plain'},
        verify=False
      )
      hackpad = r.json
    else:
      r = requests.get(
        api_link,
        headers={'Content-Type': 'text/plain'},
        verify=False
      )
      hackpad = r.json
  except:
    logging.info(sys.exc_info()[0])
    hackpad = {}

  return hackpad
示例#3
0
def do_api_request(path, method, post_data={}, body=''):
    try:
        url = urljoin(
            'https://%s.hackpad.com/api/1.0/' % settings.get('hackpad_domain'),
            path)
        args = dict(
            oauth_consumer_key=escape.to_basestring(
                settings.get('hackpad_oauth_client_id')),
            oauth_signature_method='HMAC-SHA1',
            oauth_timestamp=str(int(time.time())),
            oauth_nonce=escape.to_basestring(
                binascii.b2a_hex(uuid.uuid4().bytes)),
            oauth_version='1.0a',
        )
        signature = _oauth10a_signature(
            {
                'key': settings.get('hackpad_oauth_client_id'),
                'secret': settings.get('hackpad_oauth_secret')
            }, method, url, args)
        args['oauth_signature'] = signature
        api_link = url + '?' + urllib.urlencode(args)
        logging.info(api_link)

        hackpad = {}
        if method.lower() == 'post':
            r = requests.post(api_link,
                              data=body,
                              headers={'Content-Type': 'text/plain'},
                              verify=False)
            hackpad = r.json
        else:
            r = requests.get(api_link,
                             headers={'Content-Type': 'text/plain'},
                             verify=False)
            hackpad = r.json
    except:
        logging.info(sys.exc_info()[0])
        hackpad = {}

    return hackpad