Python urlparse.urlencode() Examples

The following are 6 code examples of urlparse.urlencode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urlparse , or try the search function .
Example #1
Source File: rpchelper.py    From identity-toolkit-python-client with Apache License 2.0 6 votes vote down vote up
def _GetAccessToken(self):
    """Gets oauth2 access token for Gitkit API using service account.

    Returns:
      string, oauth2 access token.
    """
    d = {
        'assertion': self._GenerateAssertion(),
        'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    }
    try:
        body = parse.urlencode(d)
    except AttributeError:
        body = urllib.urlencode(d)
    req = urllib_request.Request(RpcHelper.TOKEN_ENDPOINT)
    req.add_header('Content-type', 'application/x-www-form-urlencoded')
    binary_body = body.encode('utf-8')
    raw_response = urllib_request.urlopen(req, binary_body)
    return simplejson.loads(raw_response.read())['access_token'] 
Example #2
Source File: billing_usage.py    From api-kickstart with Apache License 2.0 6 votes vote down vote up
def getCsvReport(product_list, startdate, enddate, source_obj):
        print
        print ("Requesting a csv report for the given time period")
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}
        path = "/billing-usage/v1/contractUsageData/csv"

        parameters = {  "reportSources" :[source_obj],
			"products"	:product_list,
                        "startDate"     :startdate,
                        "endDate"       :enddate
                }
        print
        data_string = parse.urlencode({p: json.dumps(parameters[p]) for p in parameters})
        products_result = session.post(parse.urljoin(baseurl,path),data=data_string, headers=headers)
        products_csv = products_result.text
        return products_csv 
Example #3
Source File: backend.py    From zotero-cli with MIT License 5 votes vote down vote up
def create_api_key():
        """ Interactively create a new API key via Zotero's OAuth API.

        Requires the user to enter a verification key displayed in the browser.

        :returns:   API key and the user's library ID
        """
        auth = OAuth1Service(
            name='zotero',
            consumer_key=CLIENT_KEY,
            consumer_secret=CLIENT_SECRET,
            request_token_url=REQUEST_TOKEN_URL,
            access_token_url=ACCESS_TOKEN_URL,
            authorize_url=AUTH_URL,
            base_url=BASE_URL)
        token, secret = auth.get_request_token(
            params={'oauth_callback': 'oob'})
        auth_url = auth.get_authorize_url(token)
        auth_url += '&' + urlencode({
            'name': 'zotero-cli',
            'library_access': 1,
            'notes_access': 1,
            'write_access': 1,
            'all_groups': 'read'})
        click.echo("Opening {} in browser, please confirm.".format(auth_url))
        click.launch(auth_url)
        verification = click.prompt("Enter verification code")
        token_resp = auth.get_raw_access_token(
            token, secret, method='POST',
            data={'oauth_verifier': verification})
        if not token_resp:
            logging.debug(token_resp.content)
            click.fail("Error during API key generation.")
        access = urlparse.parse_qs(token_resp.text)
        return access['oauth_token'][0], access['userID'][0] 
Example #4
Source File: gitkitclient.py    From identity-toolkit-python-client with Apache License 2.0 5 votes vote down vote up
def _BuildOobLink(self, param, mode):
    """Builds out-of-band URL.

    Gitkit API GetOobCode() is called and the returning code is combined
    with Gitkit widget URL to building the out-of-band url.

    Args:
      param: dict of request.
      mode: string, Gitkit widget mode to handle the oob action after user
          clicks the oob url in the email.

    Raises:
      GitkitClientError: if oob code is not returned.

    Returns:
      A string of oob url.
    """
    code = self.rpc_helper.GetOobCode(param)
    if code:
      parsed = list(parse.urlparse(self.widget_url))

      query = dict(parse.parse_qsl(parsed[4]))
      query.update({'mode': mode, 'oobCode': code})

      try:
        parsed[4] = parse.urlencode(query)
      except AttributeError:
        parsed[4] = urllib.urlencode(query)

      return code, parse.urlunparse(parsed)
    raise errors.GitkitClientError('invalid request') 
Example #5
Source File: connector.py    From infoblox-client with Apache License 2.0 5 votes vote down vote up
def __init__(self, options):
        self._parse_options(options)
        self._configure_session()
        # urllib has different interface for py27 and py34
        try:
            self._urlencode = urllib.urlencode
            self._quote = urllib.quote
            self._urljoin = urlparse.urljoin
        except AttributeError:
            self._urlencode = urlparse.urlencode
            self._quote = urlparse.quote
            self._urljoin = urlparse.urljoin 
Example #6
Source File: billing_usage.py    From api-kickstart with Apache License 2.0 5 votes vote down vote up
def getProducts(parameter_obj,startdate,enddate):
	print
	print ("Requesting a list of products for the given time period")
	headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8','Accept':'application/json'}
	path = "/billing-usage/v1/products"

	parameters = {	"reportSources"	:parameter_obj,
			"startDate"	:startdate,
			"endDate"	:enddate
		}
  
	data_string = encoder.urlencode({p: json.dumps(parameters[p]) for p in parameters})
	products_result = session.post(parse.urljoin(baseurl,path),data=data_string, headers=headers)
	products_obj = json.loads(products_result.text)
	return products_obj['contents']