Python urllib.request.headers() Examples

The following are 28 code examples of urllib.request.headers(). 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 urllib.request , or try the search function .
Example #1
Source File: gauth.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
        """Sets the Authorization header in the HTTP request using the token.

        Calculates an RSA signature using the information in the token to
        indicate that the request came from this application and that this
        application has permission to access a particular user's data.

        Returns:
          The same HTTP request object which was passed in.
        """
        timestamp = str(int(time.time()))
        nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
        signature = generate_rsa_signature(
            http_request, self.consumer_key, self.rsa_private_key, timestamp,
            nonce, version='1.0', next=self.next, token=self.token,
            token_secret=self.token_secret, verifier=self.verifier)
        http_request.headers['Authorization'] = generate_auth_header(
            self.consumer_key, timestamp, nonce, RSA_SHA1, signature,
            version='1.0', next=self.next, token=self.token,
            verifier=self.verifier)
        return http_request 
Example #2
Source File: gauth.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
        """Sets the Authorization header in the HTTP request using the token.

        Calculates an HMAC signature using the information in the token to
        indicate that the request came from this application and that this
        application has permission to access a particular user's data.

        Returns:
          The same HTTP request object which was passed in.
        """
        timestamp = str(int(time.time()))
        nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
        signature = generate_hmac_signature(
            http_request, self.consumer_key, self.consumer_secret, timestamp,
            nonce, version='1.0', next=self.next, token=self.token,
            token_secret=self.token_secret, verifier=self.verifier)
        http_request.headers['Authorization'] = generate_auth_header(
            self.consumer_key, timestamp, nonce, HMAC_SHA1, signature,
            version='1.0', next=self.next, token=self.token,
            verifier=self.verifier)
        return http_request 
Example #3
Source File: gauth.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def upgrade_to_access_token(request_token, server_response_body):
    """Extracts access token information from response to an upgrade request.

    Once the server has responded with the new token info for the OAuth
    access token, this method modifies the request_token to set and unset
    necessary fields to create valid OAuth authorization headers for requests.

    Args:
      request_token: An OAuth token which this function modifies to allow it
          to be used as an access token.
      server_response_body: str The server's response to an OAuthAuthorizeToken
          request. This should contain the new token and token_secret which
          are used to generate the signature and parameters of the Authorization
          header in subsequent requests to Google Data APIs.

    Returns:
      The same token object which was passed in.
    """
    token, token_secret = oauth_token_info_from_body(server_response_body)
    request_token.token = token
    request_token.token_secret = token_secret
    request_token.auth_state = ACCESS_TOKEN
    request_token.next = None
    request_token.verifier = None
    return request_token 
Example #4
Source File: gauth.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
        """Sets the Authorization header and includes a digital signature.

        Calculates a digital signature using the private RSA key, a timestamp
        (uses now at the time this method is called) and a random nonce.

        Args:
          http_request: The atom.http_core.HttpRequest which contains all of the
              information needed to send a request to the remote server. The
              URL and the method of the request must be already set and cannot be
              changed after this token signs the request, or the signature will
              not be valid.
        """
        timestamp = str(int(time.time()))
        nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
        data = build_auth_sub_data(http_request, timestamp, nonce)
        signature = generate_signature(data, self.rsa_private_key)
        http_request.headers['Authorization'] = (
            '%s%s sigalg="rsa-sha1" data="%s" sig="%s"' % (AUTHSUB_AUTH_LABEL,
                                                           self.token_string, data, signature)) 
Example #5
Source File: gauth.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
    """Sets the Authorization header and includes a digital signature.

    Calculates a digital signature using the private RSA key, a timestamp
    (uses now at the time this method is called) and a random nonce.

    Args:
      http_request: The atom.http_core.HttpRequest which contains all of the
          information needed to send a request to the remote server. The
          URL and the method of the request must be already set and cannot be
          changed after this token signs the request, or the signature will
          not be valid.
    """
    timestamp = str(int(time.time()))
    nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
    data = build_auth_sub_data(http_request, timestamp, nonce)
    signature = generate_signature(data, self.rsa_private_key)
    http_request.headers['Authorization'] = (
        '%s%s sigalg="rsa-sha1" data="%s" sig="%s"' % (AUTHSUB_AUTH_LABEL,
            self.token_string, data, signature)) 
Example #6
Source File: gauth.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def generate_request_for_access_token(
    request_token, auth_server_url=ACCESS_TOKEN_URL):
  """Creates a request to ask the OAuth server for an access token.

  Requires a request token which the user has authorized. See the
  documentation on OAuth with Google Data for more details:
  http://code.google.com/apis/accounts/docs/OAuth.html#AccessToken

  Args:
    request_token: An OAuthHmacToken or OAuthRsaToken which the user has
        approved using their browser.
    auth_server_url: (optional) The URL at which the OAuth access token is
        requested. Defaults to
        https://www.google.com/accounts/OAuthGetAccessToken

  Returns:
    A new HttpRequest object which can be sent to the OAuth server to
    request an OAuth Access Token.
  """
  http_request = atom.http_core.HttpRequest(auth_server_url, 'POST')
  http_request.headers['Content-Length'] = '0'
  return request_token.modify_request(http_request) 
Example #7
Source File: gauth.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
    """Sets the Authorization header in the HTTP request using the token.

    Calculates an HMAC signature using the information in the token to
    indicate that the request came from this application and that this
    application has permission to access a particular user's data.

    Returns:
      The same HTTP request object which was passed in.
    """
    timestamp = str(int(time.time()))
    nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
    signature = generate_hmac_signature(
        http_request, self.consumer_key, self.consumer_secret, timestamp,
        nonce, version='1.0', next=self.__next__, token=self.token,
        token_secret=self.token_secret, verifier=self.verifier)
    http_request.headers['Authorization'] = generate_auth_header(
        self.consumer_key, timestamp, nonce, HMAC_SHA1, signature,
        version='1.0', next=self.__next__, token=self.token,
        verifier=self.verifier)
    return http_request 
Example #8
Source File: gauth.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def modify_request(self, http_request):
    """Sets the Authorization header in the HTTP request using the token.

    Calculates an RSA signature using the information in the token to
    indicate that the request came from this application and that this
    application has permission to access a particular user's data.

    Returns:
      The same HTTP request object which was passed in.
    """
    timestamp = str(int(time.time()))
    nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
    signature = generate_rsa_signature(
        http_request, self.consumer_key, self.rsa_private_key, timestamp,
        nonce, version='1.0', next=self.__next__, token=self.token,
        token_secret=self.token_secret, verifier=self.verifier)
    http_request.headers['Authorization'] = generate_auth_header(
        self.consumer_key, timestamp, nonce, RSA_SHA1, signature,
        version='1.0', next=self.__next__, token=self.token,
        verifier=self.verifier)
    return http_request 
Example #9
Source File: gauth.py    From gdata-python3 with Apache License 2.0 6 votes vote down vote up
def __init__(self, http_response, response_body=None):
        """Sets the HTTP information in the error.

        Args:
          http_response: The response from the server, contains error information.
          response_body: string (optional) specified if the response has already
                         been read from the http_response object.
        """
        body = response_body or http_response.read()

        self.status = http_response.status
        self.reason = http_response.reason
        self.body = body
        self.headers = atom.http_core.get_headers(http_response)

        self.error_msg = 'Invalid response %s.' % self.status
        try:
            json_from_body = simplejson.loads(body.decode('utf-8'))
            if isinstance(json_from_body, dict):
                self.error_msg = json_from_body.get('error', self.error_msg)
        except (ValueError, JSONDecodeError):
            pass 
Example #10
Source File: distributed_scheduler.py    From scrapy-cluster with MIT License 6 votes vote down vote up
def request_to_dict(self, request):
        '''
        Convert Request object to a dict.
        modified from scrapy.utils.reqser
        '''
        req_dict = {
            # urls should be safe (safe_string_url)
            'url': to_unicode(request.url),
            'method': request.method,
            'headers': dict(request.headers),
            'body': request.body,
            'cookies': request.cookies,
            'meta': request.meta,
            '_encoding': request._encoding,
            'priority': request.priority,
            'dont_filter': request.dont_filter,
             #  callback/errback are assumed to be a bound instance of the spider
            'callback': None if request.callback is None else request.callback.__name__,
            'errback': None if request.errback is None else request.errback.__name__,
        }
        return req_dict 
Example #11
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def get_access_token(self, code):
    """Exhanges a code for an access token.

    Args:
      code: string or dict, either the code as a string, or a dictionary
        of the query parameters to the redirect_uri, which contains
        the code.
    """

    if not (isinstance(code, str) or isinstance(code, str)):
      code = code['code']

    body = urllib.parse.urlencode({
      'grant_type': 'authorization_code',
      'client_id': self.client_id,
      'client_secret': self.client_secret,
      'code': code,
      'redirect_uri': self.redirect_uri,
      'scope': self.scope
      })
    headers = {
      'user-agent': self.user_agent,
    }
    http_client = atom.http_core.HttpClient()
    http_request = atom.http_core.HttpRequest(uri=self.token_uri, method='POST',
                                              headers=headers)
    http_request.add_body_part(data=body, mime_type='application/x-www-form-urlencoded')
    response = http_client.request(http_request)
    body = response.read()
    if response.status == 200:
      self._extract_tokens(body)
      return self
    else:
      error_msg = 'Invalid response %s.' % response.status
      try:
        d = simplejson.loads(body)
        if 'error' in d:
          error_msg = d['error']
      except:
        pass
      raise OAuth2AccessTokenError(error_msg) 
Example #12
Source File: cacheObject.py    From osm-python-tools with GNU General Public License v3.0 5 votes vote down vote up
def __query(self, requestString, params):
        request = self._queryRequest(self._endpoint, requestString, params=params)
        if not isinstance(request, urllib.request.Request):
            request = urllib.request.Request(request)
        request.headers['User-Agent'] = self._userAgent()
        try:
            response = urllib.request.urlopen(request)
        except urllib.request.HTTPError as err:
            raise Exception('The requested data could not be downloaded. ' + str(err))
        except:
            raise Exception('The requested data could not be downloaded.  Please check whether your internet connection is working.')
        encoding = response.info().get_content_charset('utf-8')
        r = response.read().decode(encoding)
        return ujson.loads(r) if self.__jsonResult else r 
Example #13
Source File: gauth.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def _refresh(self, request):
        """Refresh the access_token using the refresh_token.

        Args:
          request: The atom.http_core.HttpRequest which contains all of the
              information needed to send a request to the remote server.
        """
        body = urllib.parse.urlencode({
            'grant_type': 'refresh_token',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'refresh_token': self.refresh_token
        })
        headers = {
            'user-agent': self.user_agent,
        }

        http_request = atom.http_core.HttpRequest(
            uri=self.token_uri, method='POST', headers=headers)
        http_request.add_body_part(
            body, mime_type='application/x-www-form-urlencoded')
        response = request(http_request)
        body = response.read()
        if response.status == 200:
            self._extract_tokens(body)
        else:
            self._invalid = True
        return response 
Example #14
Source File: gauth.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def modify_request(self, http_request):
        """Sets Authorization header, allows app to act on the user's behalf."""
        http_request.headers['Authorization'] = '%s%s' % (AUTHSUB_AUTH_LABEL,
                                                          self.token_string) 
Example #15
Source File: gauth.py    From gdata-python3 with Apache License 2.0 5 votes vote down vote up
def modify_request(self, http_request):
        http_request.headers['Authorization'] = '{}{}'.format(PROGRAMMATIC_AUTH_LABEL, str(self.token_string, 'ascii')) 
Example #16
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def modify_request(self, http_request):
    """Sets the Authorization header in the HTTP request using the token.

    Returns:
      The same HTTP request object which was passed in.
    """
    http_request.headers['Authorization'] = '%s%s' % (OAUTH2_AUTH_LABEL, self.access_token)
    return http_request 
Example #17
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _refresh(self, request):
    """Refresh the access_token using the refresh_token.

    Args:
       http: An instance of httplib2.Http.request
           or something that acts like it.
    """
    body = urllib.parse.urlencode({
      'grant_type': 'refresh_token',
      'client_id': self.client_id,
      'client_secret': self.client_secret,
      'refresh_token' : self.refresh_token
      })
    headers = {
        'user-agent': self.user_agent,
    }

    http_request = atom.http_core.HttpRequest(
        uri=self.token_uri, method='POST', headers=headers)
    http_request.add_body_part(
        body, mime_type='application/x-www-form-urlencoded')
    response = request(http_request)
    body = response.read()
    if response.status == 200:
      self._extract_tokens(body)
    else:
      self._invalid = True
    return response 
Example #18
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def modify_request(self, http_request):
    """Sets Authorization header, allows app to act on the user's behalf."""
    http_request.headers['Authorization'] = '%s%s' % (AUTHSUB_AUTH_LABEL,
        self.token_string) 
Example #19
Source File: gauth.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def modify_request(self, http_request):
    http_request.headers['Authorization'] = '%s%s' % (PROGRAMMATIC_AUTH_LABEL,
        self.token_string) 
Example #20
Source File: http_helpers.py    From shavar with Mozilla Public License 2.0 5 votes vote down vote up
def proxy(request, scheme, netloc, timeout=5):
    """Proxies and return the result from the other server.

    - scheme: http or https
    - netloc: proxy location
    """
    parsed = urlparse(request.url)
    path = parsed.path
    params = parsed.params
    query = parsed.query
    fragment = parsed.fragment
    url = urlunparse((scheme, netloc, path, params, query, fragment))
    method = request.method
    data = request.body

    # copying all X- headers
    xheaders = {}
    for header, value in list(request.headers.items()):
        if not header.startswith('X-'):
            continue
        xheaders[header] = value

    if 'X-Forwarded-For' not in request.headers:
        xheaders['X-Forwarded-For'] = request.remote_addr

    if hasattr(request, '_authorization'):
        xheaders['Authorization'] = request._authorization

    status, headers, body = get_url(url, method, data, timeout=timeout,
                                    extra_headers=xheaders)

    return Response(body, status, list(headers.items())) 
Example #21
Source File: web.py    From mailur with GNU General Public License v3.0 5 votes vote down vote up
def nginx():
    h = request.headers
    try:
        login, pw = h['Auth-User'], h['Auth-Pass']
        protocol = h['Auth-Protocol']
    except KeyError as e:
        return abort(400, repr(e))

    if login in conf['IMAP_OFF']:
        response.set_header('Auth-Status', 'Disabled')
        response.set_header('Auth-Wait', 3)
        return ''

    port = {'imap': '143', 'smtp': '25'}[protocol]

    try:
        local.connect(login, pw)
        response.set_header('Auth-Status', 'OK')
        response.set_header('Auth-Server', '127.0.0.1')
        response.set_header('Auth-Port', port)
    except imap.Error as e:
        response.set_header('Auth-Status', str(e))
        response.set_header('Auth-Wait', 3)
    return '' 
Example #22
Source File: web.py    From mailur with GNU General Public License v3.0 5 votes vote down vote up
def session(callback):
    cookie_name = 'session'
    serializer = URLSafeSerializer(conf['SECRET'])

    def inner(*args, **kwargs):
        data_raw = data = request.get_cookie(cookie_name)
        if data_raw:
            try:
                data = serializer.loads(data_raw)
            except (BadSignature, BadData):
                data = None

        if data:
            conf['USER'] = data['username']

        request.session = data or {}

        try:
            return callback(*args, **kwargs)
        finally:
            if request.session:
                save(request.session)
            elif not data_raw:
                pass
            else:
                response.delete_cookie(cookie_name)

    def save(session):
        cookie_opts = {
            # keep session for 3 days
            'max_age': 3600 * 24 * 3,

            # for security
            'httponly': True,
            'secure': request.headers.get('X-Forwarded-Proto') == 'https',
        }
        data = serializer.dumps(session)
        response.set_cookie(cookie_name, data, **cookie_opts)
    return inner 
Example #23
Source File: test_sync_client_hooks.py    From opentracing-python-instrumentation with MIT License 4 votes vote down vote up
def test_urllib2(urllibver, scheme, root_span, install_hooks, tracer):

    module = install_hooks

    if module is None:
        pytest.skip('Skipping %s on Py3' % urllibver)

    class Response(object):
        def __init__(self):
            self.code = 200
            self.msg = ''

        def info(self):
            return None

    if root_span:
        root_span = tracer.start_span('root-span')
    else:
        root_span = None

    # ideally we should have started a test server and tested with real HTTP
    # request, but doing that for https is more difficult, so we mock the
    # request sending part.
    if urllibver == 'urllib2':
        p_do_open = mock.patch(
            'urllib2.AbstractHTTPHandler.do_open', return_value=Response()
        )
    else:
        cls = module.AbstractHTTPHandler
        p_do_open = mock.patch.object(
            cls, 'do_open', return_value=Response()
        )

    with p_do_open, span_in_context(span=root_span):
        request = module.Request(
            '%s://localhost:9777/proxy' % scheme,
            headers={
                'Remote-LOC': 'New New York',
                'Remote-Op': 'antiquing'
            })
        resp = module.urlopen(request)

    assert resp.code == 200
    assert len(tracer.recorder.get_spans()) == 1

    span = tracer.recorder.get_spans()[0]
    assert span.tags.get('span.kind') == 'client'

    # verify trace-id was correctly injected into headers
    # we wrap the headers to avoid having to deal with upper/lower case
    norm_headers = HTTPHeaders(request.headers)
    trace_id_header = norm_headers.get('ot-tracer-traceid')
    assert trace_id_header == '%x' % span.context.trace_id 
Example #24
Source File: gauth.py    From python-for-android with Apache License 2.0 4 votes vote down vote up
def generate_request_for_request_token(
    consumer_key, signature_type, scopes, rsa_key=None, consumer_secret=None,
    auth_server_url=REQUEST_TOKEN_URL, next='oob', version='1.0'):
  """Creates request to be sent to auth server to get an OAuth request token.

  Args:
    consumer_key:
    signature_type: either RSA_SHA1 or HMAC_SHA1. The rsa_key must be
        provided if the signature type is RSA but if the signature method
        is HMAC, the consumer_secret must be used.
    scopes: List of URL prefixes for the data which we want to access. For
        example, to request access to the user's Blogger and Google Calendar
        data, we would request
        ['http://www.blogger.com/feeds/',
         'https://www.google.com/calendar/feeds/',
         'http://www.google.com/calendar/feeds/']
    rsa_key: Only used if the signature method is RSA_SHA1.
    consumer_secret: Only used if the signature method is HMAC_SHA1.
    auth_server_url: The URL to which the token request should be directed.
        Defaults to 'https://www.google.com/accounts/OAuthGetRequestToken'.
    next: The URL of the page that the user's browser should be sent to
        after they authorize the token. (Optional)
    version: The OAuth version used by the requesting web application.
        Defaults to '1.0a'

  Returns:
    An atom.http_core.HttpRequest object with the URL, Authorization header
    and body filled in.
  """
  request = atom.http_core.HttpRequest(auth_server_url, 'POST')
  # Add the requested auth scopes to the Auth request URL.
  if scopes:
    request.uri.query['scope'] = ' '.join(scopes)

  timestamp = str(int(time.time()))
  nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
  signature = None
  if signature_type == HMAC_SHA1:
    signature = generate_hmac_signature(
        request, consumer_key, consumer_secret, timestamp, nonce, version,
        next=next)
  elif signature_type == RSA_SHA1:
    signature = generate_rsa_signature(
        request, consumer_key, rsa_key, timestamp, nonce, version, next=next)
  else:
    return None

  request.headers['Authorization'] = generate_auth_header(
      consumer_key, timestamp, nonce, signature_type, signature, version,
      next)
  request.headers['Content-Length'] = '0'
  return request 
Example #25
Source File: gauth.py    From gdata-python3 with Apache License 2.0 4 votes vote down vote up
def generate_request_for_request_token(
        consumer_key, signature_type, scopes, rsa_key=None, consumer_secret=None,
        auth_server_url=REQUEST_TOKEN_URL, next='oob', version='1.0'):
    """Creates request to be sent to auth server to get an OAuth request token.

    Args:
      consumer_key:
      signature_type: either RSA_SHA1 or HMAC_SHA1. The rsa_key must be
          provided if the signature type is RSA but if the signature method
          is HMAC, the consumer_secret must be used.
      scopes: List of URL prefixes for the data which we want to access. For
          example, to request access to the user's Blogger and Google Calendar
          data, we would request
          ['http://www.blogger.com/feeds/',
           'https://www.google.com/calendar/feeds/',
           'http://www.google.com/calendar/feeds/']
      rsa_key: Only used if the signature method is RSA_SHA1.
      consumer_secret: Only used if the signature method is HMAC_SHA1.
      auth_server_url: The URL to which the token request should be directed.
          Defaults to 'https://www.google.com/accounts/OAuthGetRequestToken'.
      next: The URL of the page that the user's browser should be sent to
          after they authorize the token. (Optional)
      version: The OAuth version used by the requesting web application.
          Defaults to '1.0a'

    Returns:
      An atom.http_core.HttpRequest object with the URL, Authorization header
      and body filled in.
    """
    request = atom.http_core.HttpRequest(auth_server_url, 'POST')
    # Add the requested auth scopes to the Auth request URL.
    if scopes:
        request.uri.query['scope'] = ' '.join(scopes)

    timestamp = str(int(time.time()))
    nonce = ''.join([str(random.randint(0, 9)) for i in range(15)])
    signature = None
    if signature_type == HMAC_SHA1:
        signature = generate_hmac_signature(
            request, consumer_key, consumer_secret, timestamp, nonce, version,
            next=next)
    elif signature_type == RSA_SHA1:
        signature = generate_rsa_signature(
            request, consumer_key, rsa_key, timestamp, nonce, version, next=next)
    else:
        return None

    request.headers['Authorization'] = generate_auth_header(
        consumer_key, timestamp, nonce, signature_type, signature, version,
        next)
    request.headers['Content-Length'] = '0'
    return request 
Example #26
Source File: proxy.py    From maloja with GNU General Public License v3.0 4 votes vote down vote up
def instructions(keys):
	authenticated = False
	if "Cookie" in request.headers:
		cookies = request.headers["Cookie"].split(";")
		for c in cookies:
			if c.strip().startswith("apikey="):
				authenticated = checkAPIkey(c.strip()[7:])

	if "token" in keys and authenticated:
		token = keys.get("token")
		parameters = {
			"method":"auth.getSession",
			"token":token,
			"api_key":get_settings("LASTFM_API_KEY")
		}
		response = urllib.request.urlopen("http://ws.audioscrobbler.com/2.0/?" + lfmbuild(parameters))
		xml = response.read()
		data = ET.fromstring(xml)
		if data.attrib.get("status") == "ok":
			username = data.find("session").find("name").text
			sessionkey = data.find("session").find("key").text

			update_settings("settings/settings.ini",{"LASTFM_API_SK":sessionkey,"LASTFM_USERNAME":username},create_new=True)

		return "/proxy"

	else:
		key,secret,sessionkey,name = get_settings("LASTFM_API_KEY","LASTFM_API_SECRET","LASTFM_API_SK","LASTFM_USERNAME")

		if key is None:
			lastfm = "<td>No Last.fm key provided</td>"
		elif secret is None:
			lastfm = "<td>No Last.fm secret provided</td>"
		elif sessionkey is None and authenticated:
			url = "http://www.last.fm/api/auth/?api_key=" + key + "&cb="
			lastfm = "<td class='button'><a id='lastfmlink' href='" + url + "'><div>Connect</div></a></td>"
		elif sessionkey is None:
			lastfm = "<td>Not active</td>"
		else:

			lastfm = "<td>Account: " + name + "</td>"



	return {"KEY_STATUS_LASTFM":lastfm},[] 
Example #27
Source File: gauth.py    From gdata-python3 with Apache License 2.0 4 votes vote down vote up
def get_access_token(self, code):
        """Exhanges a code for an access token.

        Args:
          code: string or dict, either the code as a string, or a dictionary
            of the query parameters to the redirect_uri, which contains
            the code.
        """

        if not (isinstance(code, str) or isinstance(code, str)):
            code = code['code']

        body = urllib.parse.urlencode({
            'grant_type': 'authorization_code',
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'code': code,
            'redirect_uri': self.redirect_uri,
            'scope': self.scope
        })
        headers = {
            'user-agent': self.user_agent,
        }
        http_client = atom.http_core.HttpClient()
        http_request = atom.http_core.HttpRequest(uri=self.token_uri, method='POST',
                                                  headers=headers)
        http_request.add_body_part(data=body,
                                   mime_type='application/x-www-form-urlencoded')
        response = http_client.request(http_request)
        body = response.read()
        if response.status == 200:
            self._extract_tokens(body)
            return self
        else:
            error_msg = 'Invalid response %s.' % response.status
            try:
                d = simplejson.loads(body.decode('utf-8'))
                if 'error' in d:
                    error_msg = d['error']
            except:
                pass
            raise OAuth2AccessTokenError(error_msg) 
Example #28
Source File: binary_utils.py    From RLs with Apache License 2.0 4 votes vote down vote up
def download_and_extract_zip(url: str, name: str) -> None:
    """
    Downloads a zip file under a URL, extracts its contents into a folder with the name
    argument and gives chmod 755 to all the files it contains. Files are downloaded and
    extracted into special folders in the temp folder of the machine.
    :param url: The URL of the zip file
    :param name: The name that will be given to the folder containing the extracted data
    """
    zip_dir, bin_dir = get_tmp_dir()
    url_hash = "-" + hashlib.md5(url.encode()).hexdigest()
    binary_path = os.path.join(bin_dir, name + url_hash)
    if os.path.exists(binary_path):
        shutil.rmtree(binary_path)

    # Download zip
    try:
        request = urllib.request.urlopen(url, timeout=30)
    except urllib.error.HTTPError as e:  # type: ignore
        e.msg += " " + url
        raise
    zip_size = int(request.headers["content-length"])
    zip_file_path = os.path.join(zip_dir, str(uuid.uuid4()) + ".zip")
    with open(zip_file_path, "wb") as zip_file:
        downloaded = 0
        while True:
            buffer = request.read(BLOCK_SIZE)
            if not buffer:
                # There is nothing more to read
                break
            downloaded += len(buffer)
            zip_file.write(buffer)
            downloaded_percent = downloaded / zip_size * 100
            print_progress(f"  Downloading {name}", downloaded_percent)
        print("")

    # Extraction
    with ZipFileWithProgress(zip_file_path, "r") as zip_ref:
        zip_ref.extract_zip(f"  Extracting  {name}", binary_path)  # type: ignore
    print("")

    # Clean up zip
    print_progress(f"  Cleaning up {name}", 0)
    os.remove(zip_file_path)

    # Give permission
    for f in glob.glob(binary_path + "/**/*", recursive=True):
        # 16877 is octal 40755, which denotes a directory with permissions 755
        os.chmod(f, 16877)
    print_progress(f"  Cleaning up {name}", 100)
    print("")