def request(self, uri, method="GET", body=None, headers=None, redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None): if not isinstance(headers, dict): headers = {} if body and method == "POST": parameters = dict(parse_qsl(body)) elif method == "GET": parsed = urlparse.urlparse(uri) parameters = parse_qsl(parsed.query) else: parameters = None req = Request.from_consumer_and_token(self.consumer, token=self.token, http_method=method, http_url=uri, parameters=parameters) req.sign_request(self.method, self.consumer, self.token) if method == "POST": body = req.to_postdata() headers['Content-Type'] = 'application/x-www-form-urlencoded' elif method == "GET": uri = req.to_url() else: headers.update(req.to_header()) return httplib2.Http.request(self, uri, method=method, body=body, headers=headers, redirections=redirections, connection_type=connection_type)
def _mgmt(self): '''mgmt interface to parse the url ''' ms_dict = {} try: req_str = urlparse.urlparse(self.path) com_list = req_str.query.split(r'/') cmd_dict = dict(urlparse.parse_qsl(com_list[0])) if len(com_list) > 1: ms_dict = dict(urlparse.parse_qsl(com_list[1])) except (AttributeError, TypeError, KeyError, IndexError): return 'return=error\r\nerrorinfo=wrongurlpath' try: req_cmd = cmd_dict['cmd'] # print req_cmd except (IndexError, TypeError): return 'return=error\r\nerror=errorcmd' if ms_dict.has_key("num"): num = ms_dict["num"] else: num = None handler_name = '_mgmt_'+req_cmd if not hasattr(self, handler_name): return 'return=error\r\nerrorinfo=unknowncmd' handler_func = getattr(self, handler_name) return handler_func(num)
def get_download_url_ssl(self): """ SSL-enabled links should always be used except Windows stub installers. """ # SSL-enabled links won't be used for Windows builds (but SSL download # is enabled by default for stub installers) url = firefox_desktop.get_download_url('release', '27.0', 'win', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0'), ('os', 'win'), ('lang', 'pt-BR')]) # SSL-enabled links will be used for OS X builds url = firefox_desktop.get_download_url('release', '27.0', 'osx', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0-SSL'), ('os', 'osx'), ('lang', 'pt-BR')]) # SSL-enabled links will be used for Linux builds url = firefox_desktop.get_download_url('release', '27.0', 'linux', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0-SSL'), ('os', 'linux'), ('lang', 'pt-BR')])
def get_download_url_ssl(self): """ SSL-enabled links should be used for the specific verions, except the Windows stub installers. """ # SSL-enabled links won't be used for 26.0 url = firefox_details.get_download_url("OS X", "pt-BR", "26.0") self.assertListEqual( parse_qsl(urlparse(url).query), [("product", "firefox-26.0"), ("os", "osx"), ("lang", "pt-BR")] ) # SSL-enabled links won't be used for 27.0 Windows builds (but SSL # download is enabled by default for stub installers) url = firefox_details.get_download_url("Windows", "pt-BR", "27.0") self.assertListEqual( parse_qsl(urlparse(url).query), [("product", "firefox-27.0"), ("os", "win"), ("lang", "pt-BR")] ) # SSL-enabled links will be used for 27.0 OS X builds url = firefox_details.get_download_url("OS X", "pt-BR", "27.0") self.assertListEqual( parse_qsl(urlparse(url).query), [("product", "firefox-27.0-SSL"), ("os", "osx"), ("lang", "pt-BR")] ) # SSL-enabled links will be used for 27.0 Linux builds url = firefox_details.get_download_url("Linux", "pt-BR", "27.0") self.assertListEqual( parse_qsl(urlparse(url).query), [("product", "firefox-27.0-SSL"), ("os", "linux"), ("lang", "pt-BR")] )
def collect_parameters(uri_query=None, authorization_header=None, body=None, exclude_oauth_signature=True): """Collect parameters from the uri query, authorization header, and request body. Per `section 3.4.1.3.1`_ of the spec. .. _`section 3.4.1.3.1`: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1 """ params = [] if uri_query is not None: params.extend(urlparse.parse_qsl(uri_query)) if authorization_header is not None: params.extend(utils.parse_authorization_header(authorization_header)) if body is not None: params.extend(urlparse.parse_qsl(body)) exclude_params = [u"realm"] if exclude_oauth_signature: exclude_params.append(u"oauth_signature") params = filter(lambda i: i[0] not in exclude_params, params) return params
def test_get_download_url_nightly_full(self): """ The Aurora version should give us a bouncer url. For Windows, a full url should be returned. """ url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-ssl'), ('os', 'win'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win64', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-ssl'), ('os', 'win64'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'osx', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-ssl'), ('os', 'osx'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-ssl'), ('os', 'linux'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux64', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-ssl'), ('os', 'linux64'), ('lang', 'en-US')])
def test_get_download_url_devedition_full(self): """ The Developer Edition version should give us a bouncer url. For Windows, a full url should be returned. """ url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'win'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win64', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'win64'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'osx', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'osx'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'linux'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux64', 'en-US', True, True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'linux64'), ('lang', 'en-US')])
def test_get_download_url_nightly_l10n(self): """ The Nightly version should give us a bouncer url. A stub url should be returned for win32/64, while other platforms get a full url. The product name is slightly different from en-US. """ url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-stub'), ('os', 'win'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-stub'), ('os', 'win64'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'osx', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'osx'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'linux'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'linux64'), ('lang', 'pt-BR')])
def tokens(self): consumer = oauth2.Consumer(self.consumer_token, self.consumer_secret) client = oauth2.Client(consumer) # Request request tokens resp, content = client.request(self.request_token_url, "GET") if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) request_token = dict(urlparse.parse_qsl(content)) logging.debug("Request Token:\n" + " - oauth_token = {0[oauth_token]}\n".format(request_token) + " - oauth_token_secret = {0[oauth_token_secret]}\n".format(request_token)) # interactively ask for permission/connect to the account verifier = self.verifier("%s?oauth_token=%s" % (self.authorize_url, request_token['oauth_token'])) token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(verifier) # convert request token + verifier token into access token client = oauth2.Client(consumer, token) resp, content = client.request(self.access_token_url, "POST") access_token = dict(urlparse.parse_qsl(content)) return access_token
def test_get_download_url_esr(self): """ The ESR version should give us a bouncer url. There is no stub for ESR. """ url = firefox_desktop.get_download_url('esr', '28.0a2', 'win', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-esr-latest-ssl'), ('os', 'win'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr', '28.0a2', 'win64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-esr-latest-ssl'), ('os', 'win64'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr', '28.0a2', 'osx', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-esr-latest-ssl'), ('os', 'osx'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr', '28.0a2', 'linux', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-esr-latest-ssl'), ('os', 'linux'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr', '28.0a2', 'linux64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-esr-latest-ssl'), ('os', 'linux64'), ('lang', 'en-US')])
def get_access_token(consumer): client = oauth.Client(consumer) resp, content = client.request(request_token_url, "GET") if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) request_token = dict(urlparse.parse_qsl(content)) print "[ACTION] Go to the following link in your browser:" print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']) print accepted = 'n' while accepted.lower() == 'n': accepted = raw_input('Have you authorized me? (y/n) ') oauth_verifier = raw_input('What is the PIN? ') token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) resp, content = client.request(access_token_url, "POST") access_token = dict(urlparse.parse_qsl(content)) return access_token
def get_access(client): if os.path.exists("access.db"): return load_access() response, content = make_request(client,request_token_url,{},"Failed to fetch request token","POST") # parse out the tokens from the response request_token = dict(urlparse.parse_qsl(content)) print "Go to the following link in your browser:" print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']) # ask for the verifier pin code oauth_verifier = raw_input('What is the PIN? ') # swap in the new token + verifier pin token = oauth.Token(request_token['oauth_token'],request_token['oauth_token_secret']) token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) # fetch the access token response, content = make_request(client,access_token_url,{},"Failed to fetch access token","POST") # parse out the access token access_token = dict(urlparse.parse_qsl(content)) print "Access Token:" print " - oauth_token = %s" % access_token['oauth_token'] print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] print database = shelve.open("access.db") database['access'] = access_token database.close() return access_token
def oauth_requests(): auth = OAuth1(AK.API_KEY, AK.SECRET_KEY, callback_uri=callback_uri) #print auth r = requests.post(request_url, auth=auth) #print r request_token = dict(urlparse.parse_qsl(r.text)) #print request_token # Getting the User Authorization # ブラウザを開きOAuth認証確認画面を表示 # ユーザーが許可するとPINコードが表示される url = '{0}?oauth_token={1}&perms=write'.format(authorize_url, request_token['oauth_token']) #print testurl #print MI.BROWSER_PATH browser = webbrowser.get(MI.BROWSER_PATH) browser.open(url) oauth_verifier = raw_input("[PIN Code]> ") # 上記PINコードを入力する #print oauth_verifier auth = OAuth1( AK.API_KEY, AK.SECRET_KEY, request_token['oauth_token'], request_token['oauth_token_secret'], verifier=oauth_verifier) r = requests.post(access_token_url, auth=auth) access_token = dict(urlparse.parse_qsl(r.text)) return access_token
def _get_access_tokens(self): oauth_consumer = oauth2.Consumer(key=self.CONSUMER_KEY, secret=self.CONSUMER_SECRET) oauth_client = oauth2.Client(oauth_consumer) resp, content = oauth_client.request(twitter.REQUEST_TOKEN_URL, 'GET') if resp['status'] != '200': print('Invalid respond from Twitter requesting temp token: %s' % resp['status']) return request_token = dict(urlparse.parse_qsl(content)) webbrowser.open('%s?oauth_token=%s' % (twitter.AUTHORIZATION_URL, request_token['oauth_token'])) pincode = raw_input('Enter pincode') token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(pincode) oauth_client = oauth2.Client(oauth_consumer, token) resp, content = oauth_client.request(twitter.ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % pincode) if resp['status'] != '200': print('Invalid respond from Twitter requesting access token: %s' % resp['status']) return access_token = dict(urlparse.parse_qsl(content)) request_token = dict(urlparse.parse_qsl(content)) return access_token['oauth_token'], access_token['oauth_token_secret']
def test_get_download_url_aurora_l10n(self): """Aurora non en-US should have a slightly different product name.""" url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-l10n'), ('os', 'win'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-l10n'), ('os', 'win64'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'osx', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-l10n'), ('os', 'osx'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-l10n'), ('os', 'linux'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-l10n'), ('os', 'linux64'), ('lang', 'pt-BR')])
def load_oauth_token(): '''Loads a new oauth token''' consumer = oauth.Consumer(consumer_key, consumer_secret) client = oauth.Client(consumer) # Step 1: Get a request token. This is a temporary token that is used for # having the user authorize an access token and to sign the request to obtain # said access token. resp, content = client.request(request_token_url, "GET") print resp print content if resp['status'] != '200': raise Exception("Invalid response %s." % resp['status']) request_token = dict(urlparse.parse_qsl(content)) print "Request Token:" print " - oauth_token = %s" % request_token['oauth_token'] print " - oauth_token_secret = %s" % request_token['oauth_token_secret'] print # Step 2: Redirect to the provider. Since this is a CLI script we do not # redirect. In a web application you would redirect the user to the URL # below. print "Go to the following link in your browser:" print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token']) print # After the user has granted access to you, the consumer, the provider will # redirect you to whatever URL you have told them to redirect to. You can # usually define this in the oauth_callback argument as well. accepted = 'n' while accepted.lower() == 'n': accepted = raw_input('Have you authorized me? (y/n) ') oauth_verifier = raw_input('What is the PIN? ') # Step 3: Once the consumer has redirected the user back to the oauth_callback # URL you can request the access token the user has approved. You use the # request token to sign this request. After this is done you throw away the # request token and use the access token returned. You should store this # access token somewhere safe, like a database, for future use. token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) resp, content = client.request(access_token_url, "POST") access_token = dict(urlparse.parse_qsl(content)) FreelanceOAuthClient.oauth_token = access_token['oauth_token'] FreelanceOAuthClient.oauth_token_secret = access_token['oauth_token_secret'] print "Access Token:" print " - oauth_token = %s" % access_token['oauth_token'] print " - oauth_token_secret = %s" % access_token['oauth_token_secret'] print print "You may now access protected resources using the access tokens above." print
def twitter(): request_token_url = 'https://api.twitter.com/oauth/request_token' access_token_url = 'https://api.twitter.com/oauth/access_token' authenticate_url = 'https://api.twitter.com/oauth/authenticate' if request.args.get('oauth_token') and request.args.get('oauth_verifier'): auth = OAuth1(app.config['TWITTER_CONSUMER_KEY'], client_secret=app.config['TWITTER_CONSUMER_SECRET'], resource_owner_key=request.args.get('oauth_token'), verifier=request.args.get('oauth_verifier')) r = requests.post(access_token_url, auth=auth) profile = dict(parse_qsl(r.text)) user = User.query.filter_by(twitter=profile['user_id']).first() if user: token = create_token(user) return jsonify(token=token) u = User(twitter=profile['user_id'], display_name=profile['screen_name'], oauth_token=profile['oauth_token'], oauth_token_secret=profile['oauth_token_secret']) db.session.add(u) db.session.commit() token = create_token(u) return jsonify(token=token) else: oauth = OAuth1(app.config['TWITTER_CONSUMER_KEY'], client_secret=app.config['TWITTER_CONSUMER_SECRET'], callback_uri=app.config['TWITTER_CALLBACK_URL']) r = requests.post(request_token_url, auth=oauth) oauth_token = dict(parse_qsl(r.text)) qs = urlencode(dict(oauth_token=oauth_token['oauth_token'])) return redirect(authenticate_url + '?' + qs)
def build_request(en): url = urlparse.urlparse(en['request']['url']) request = dict( method = en['request']['method'], url = en['request']['url'], httpVersion = 'HTTP/1.1', headers = [ {'name': x['name'], 'value': x['value'], 'checked': True} for x in\ en['request'].get('headers', [])], queryString = [ {'name': n, 'value': v} for n, v in\ urlparse.parse_qsl(url.query)], cookies = [ {'name': x['name'], 'value': x['value'], 'checked': True} for x in\ en['request'].get('cookies', [])], headersSize = -1, bodySize = len(en['request'].get('data')) if en['request'].get('data') else 0, ) if en['request'].get('data'): request['postData'] = dict( mimeType = en['request'].get('mimeType'), text = en['request'].get('data'), ) if en['request'].get('mimeType') == 'application/x-www-form-urlencoded': params = [{'name': x[0], 'value': x[1]} \ for x in urlparse.parse_qsl(en['request']['data'], True)] request['postData']['params'] = params return request
def link_account(self, widget,consumer_key,consumer_secret): consumer_key = consumer_key.get_text() consumer_secret = consumer_secret.get_text() consumer = oauth.Consumer(consumer_key, consumer_secret) client = oauth.Client(consumer) resp, content = client.request(self.config_data['request_token_url'], "POST", urllib.urlencode({'oauth_callback':'oob'})) if resp['status'] == '200': request_token = dict(urlparse.parse_qsl(content)) oauth_verifier = self.show_dialog("Click <a href='%s?oauth_token=%s'>HERE</a>, authorize the application and copy the pin:" % (self.config_data['authorize_url'], request_token['oauth_token']),"") token = oauth.Token(request_token['oauth_token'],request_token['oauth_token_secret']) token.set_verifier(oauth_verifier) client = oauth.Client(consumer, token) resp, content = client.request(self.config_data['access_token_url'], "POST") if resp['status'] == '200': access_token = dict(urlparse.parse_qsl(content)) self.config_data['oauth_token'] = access_token['oauth_token'] self.config_data['oauth_token_secret'] = access_token['oauth_token_secret'] self.save_config_file() self.show_dialog("The application was successfully authorized.",None) else: self.show_dialog("There was an error in authorizing the application. Please verify that you granted access to the application and inputed the pin correctly.",None) else: self.show_dialog("Invalid response from server. Please verify the consumer key and secret.",None)
def test_get_download_url_esr_next(self): """ The ESR_NEXT version should give us a bouncer url with a full version. There is no stub for ESR. """ url = firefox_desktop.get_download_url('esr_next', '52.4.1esr', 'win', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-52.4.1esr-SSL'), ('os', 'win'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr_next', '52.4.1esr', 'win64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-52.4.1esr-SSL'), ('os', 'win64'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr_next', '52.4.1esr', 'osx', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-52.4.1esr-SSL'), ('os', 'osx'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr_next', '52.4.1esr', 'linux', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-52.4.1esr-SSL'), ('os', 'linux'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('esr_next', '52.4.1esr', 'linux64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-52.4.1esr-SSL'), ('os', 'linux64'), ('lang', 'en-US')])
def callback(): # Extract parameters from callback URL data = dict(parse_qsl(urlparse(request.url).query)) resource_owner = data.get(u'oauth_token').decode(u'utf-8') verifier = data.get(u'oauth_verifier').decode(u'utf-8') token_secret = session["token_secret"] # Request the access token client = OAuth1(app.config["CLIENT_KEY"], resource_owner_key=resource_owner, resource_owner_secret=token_secret, verifier=verifier, **app.config["OAUTH_CREDENTIALS"]) r = requests.post(u"http://127.0.0.1:5000/access_token", auth=client) # Extract the access token from the response data = dict(parse_qsl(r.content)) resource_owner = data.get(u'oauth_token').decode(u'utf-8') resource_owner_secret = data.get(u'oauth_token_secret').decode(u'utf-8') client = OAuth1(app.config["CLIENT_KEY"], resource_owner_key=resource_owner, resource_owner_secret=resource_owner_secret, **app.config["OAUTH_CREDENTIALS"]) r = requests.get(u"http://127.0.0.1:5000/protected", auth=client) r = requests.get(u"http://127.0.0.1:5000/protected_realm", auth=client) return r.content
def test_get_download_url_devedition_l10n(self): """ The Developer Edition version should give us a bouncer url. A stub url should be returned for win32/64, while other platforms get a full url. The product name is the same as en-US. """ url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-stub'), ('os', 'win'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-stub'), ('os', 'win64'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'osx', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'osx'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'linux'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-devedition-latest-ssl'), ('os', 'linux64'), ('lang', 'pt-BR')])
def test_get_request_token(self): """ Tests that expected responses from the LinkedIn API for get_request_token are properly handled. Known cases: success response without scope success response with scope """ with patch('linkedin_json_client.api.oauth.Client') as patched_Client: client = patched_Client.return_value client.request.return_value = ( self._responseFactory({ 'content-length': '236', }), self.request_token) # test a successful request without scope self.failUnlessEqual( self.api.get_request_token(), dict(urlparse.parse_qsl(self.request_token))) # test a successful request with self.failUnlessEqual( self.api.get_request_token( scope=[ LinkedInScope.BASIC_PROFILE, LinkedInScope.EMAIL_ADDRESS, LinkedInScope.NETWORK_UPDATES, LinkedInScope.CONNECTIONS, LinkedInScope.CONTACT_INFO, LinkedInScope.MESSAGES]), dict(urlparse.parse_qsl(self.request_token)))
def twitter(): request_token_url = 'https://api.twitter.com/oauth/request_token' access_token_url = 'https://api.twitter.com/oauth/access_token' authenticate_url = 'https://api.twitter.com/oauth/authenticate' if request.args.get('oauth_token') and request.args.get('oauth_verifier'): auth = OAuth1(app.config['OAUTH2_CLIENT_ID'], client_secret=app.config['OAUTH2_CLIENT_SECRET'], resource_owner_key=request.args.get('oauth_token'), verifier=request.args.get('oauth_verifier')) r = requests.post(access_token_url, auth=auth) profile = dict(parse_qsl(r.text)) login = profile['screen_name'] if app.config['AUTH_REQUIRED'] and not db.is_user_valid(login=login): return jsonify(status="error", message="User %s is not authorized" % login), 403 token = create_token(profile['user_id'], '@'+login, login, provider='twitter') return jsonify(token=token) else: oauth = OAuth1(app.config['OAUTH2_CLIENT_ID'], client_secret=app.config['OAUTH2_CLIENT_SECRET'], callback_uri=app.config.get('TWITTER_CALLBACK_URL', request.headers.get('Referer', '')) ) r = requests.post(request_token_url, auth=oauth) oauth_token = dict(parse_qsl(r.text)) qs = urlencode(dict(oauth_token=oauth_token['oauth_token'])) return redirect(authenticate_url + '?' + qs)
def TwitterAuth(params): consumer = oauth.Consumer(consumer_key, consumer_secret) client = oauth.Client(consumer) # step 1 - obtain a request token resp, content = client.request( 'https://api.twitter.com/oauth/request_token', 'POST') if resp['status'] != '200': print 'Failed to get request token. [%s]' % resp['status'] return 1 request_token = dict(urlparse.parse_qsl(content)) # step 2 - redirect the user redirect_url = 'https://api.twitter.com/oauth/authorize?oauth_token=%s' % ( request_token['oauth_token']) if not webbrowser.open(redirect_url, new=2, autoraise=0): print 'Please use a web browser to open the following URL' print redirect_url pin = raw_input('Please enter the PIN shown in your web browser: ').strip() # step 3 - convert the request token to an access token token = oauth.Token( request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(pin) client = oauth.Client(consumer, token) resp, content = client.request( 'https://api.twitter.com/oauth/access_token', 'POST') if resp['status'] != '200': print 'Failed to get access token. [%s]' % resp['status'] return 1 access_token = dict(urlparse.parse_qsl(content)) params.set('twitter', 'key', access_token['oauth_token']) params.set('twitter', 'secret', access_token['oauth_token_secret']) print 'Success! Authorisation data has been stored in %s' % params._path return 0
def test_get_download_url_aurora(self): """ The Aurora version should give us a bouncer url. For Windows, a stub url should be returned. """ url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-stub'), ('os', 'win'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'win64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-ssl'), ('os', 'win64'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'osx', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-ssl'), ('os', 'osx'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-ssl'), ('os', 'linux'), ('lang', 'en-US')]) url = firefox_desktop.get_download_url('alpha', '28.0a2', 'linux64', 'en-US', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-aurora-latest-ssl'), ('os', 'linux64'), ('lang', 'en-US')])
def get_download_url_ssl(self): """ SSL-enabled links should be used for the specific verions, except the Windows stub installers. """ # SSL-enabled links won't be used for 26.0 url = firefox_details.get_download_url('OS X', 'pt-BR', '26.0') self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-26.0'), ('os', 'osx'), ('lang', 'pt-BR')]) # SSL-enabled links won't be used for 27.0 Windows builds (but SSL # download is enabled by default for stub installers) url = firefox_details.get_download_url('Windows', 'pt-BR', '27.0') self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0'), ('os', 'win'), ('lang', 'pt-BR')]) # SSL-enabled links will be used for 27.0 OS X builds url = firefox_details.get_download_url('OS X', 'pt-BR', '27.0') self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0-SSL'), ('os', 'osx'), ('lang', 'pt-BR')]) # SSL-enabled links will be used for 27.0 Linux builds url = firefox_details.get_download_url('Linux', 'pt-BR', '27.0') self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-27.0-SSL'), ('os', 'linux'), ('lang', 'pt-BR')])
def test_get_download_url_nightly_l10n(self): """Nightly non en-US should have a slightly different product name.""" url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'win'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'win64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'win64'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'osx', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'osx'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'linux'), ('lang', 'pt-BR')]) url = firefox_desktop.get_download_url('nightly', '50.0a1', 'linux64', 'pt-BR', True) self.assertListEqual(parse_qsl(urlparse(url).query), [('product', 'firefox-nightly-latest-l10n-ssl'), ('os', 'linux64'), ('lang', 'pt-BR')])
def replace_or_add_query(url, query, exclusions=None): """ Adds field/value pair to the provided url as a query string if the key isn't already in the url, or replaces it otherwise. Appends the proper pair separator (?&) based on the input url Inputs: :url: URL that query string should be appended to :query: Query string(s) to add to :url: :exclusions: List of keys that should not be copied; common keys include 'vs' and 'z' Outputs: :url: Input url with query string appended """ if not exclusions: exclusions = [] if len(query) > 1 and query[0] in ['?', '&']: query = query[1:] query = query.encode('utf-8') url = url.encode('utf-8') url = urlparse.urlparse(url) old_query = urlparse.parse_qsl(url.query, keep_blank_values=True) old_keys = [q[0] for q in old_query] # make a lower-case copy of old_keys so we can do some comparisons insensitive_keys = map(str.lower, old_keys) new_query = urlparse.parse_qsl(query, keep_blank_values=True) # For each source code that we are going to add for new_index in range(len(new_query)): # Make sure we are not adding a source code that should be excluded if new_query[new_index][0] not in exclusions: try: # Case-insensitively determine if the new source code # is already applied old_index = insensitive_keys.index( new_query[new_index][0].lower()) except ValueError: # The current source code is not applied; apply it old_query.append(new_query[new_index]) else: # The current source code is already applied; replace its # value, keeping the case of the old parameter old_query[old_index] = (old_query[old_index][0], new_query[new_index][1]) # parse_qsl unencodes the query that you pass it; Re-encode the query # parameters when reconstructing the string. old_query = '&'.join(['='.join([urllib.quote(k, safe=','), urllib.quote(v, safe=',')]) for k, v in old_query]) url = url._replace(query=old_query) url = urlparse.urlunparse(url) else: parts = url.split('#') parts[0] += query url = '#'.join(parts) return url
def build_request(request): url = urlparse.urlparse(request.url) ret = dict( method = request.method, url = request.url, httpVersion = 'HTTP/1.1', headers = build_headers(request.headers), queryString = [ {'name': n, 'value': v} for n, v in\ urlparse.parse_qsl(url.query)], cookies = [ {'name': n, 'value': v} for n, v in \ urlparse.parse_qsl(request.headers.get('cookie', ''))], headersSize = -1, bodySize = len(request.body) if request.body else 0, ) if request.body: ret['postData'] = dict( mimeType = request.headers.get('content-type'), text = request.body, ) if ret['postData']['mimeType'] == 'application/x-www-form-urlencoded': ret['postData']['params'] = [ {'name': n, 'value': v} for n, v in \ urlparse.parse_qsl(request.body)] try: _ = json.dumps(ret['postData']['params']) except UnicodeDecodeError: logger.error('params encoding error') del ret['postData']['params'] return ret
import sys import xbmc try: #Py2 from urlparse import parse_qsl from urllib import quote_plus except ImportError: #Py3 from urllib.parse import parse_qsl, quote_plus if __name__ == '__main__': item = sys.listitem # message = item.getLabel() path = item.getPath() plugin = 'plugin://plugin.video.venom/' args = path.split(plugin, 1) params = dict(parse_qsl(args[1].replace('?', ''))) title = params['title'] systitle = quote_plus(title) year = params.get('year', '') if 'meta' in params: meta = jsloads(params['meta']) imdb = meta.get('imdb', '') tvdb = meta.get('tvdb', '') season = meta.get('season', '') episode = meta.get('episode', '') tvshowtitle = meta.get('tvshowtitle', '').encode('utf-8', 'ignore') systvshowtitle = quote_plus(tvshowtitle) premiered = meta.get('premiered', '')
def parse_qsl(s): return '\n'.join( "%-20s %s" % (k, v) for k, v in urlparse.parse_qsl(s, keep_blank_values=True))
def get_page(self, data=False, header=False): """ Retrieves page content from a given target URL """ headers = {} parsed_url = None page = None conn = None invalid = False parsed_url = urlsplit(self.url) if not data: parsed_url = parsed_url._replace( query=urlencode(parse_qsl(parsed_url.query))) self.url = urlunsplit(parsed_url) elif self.login_parameter_type == "json": self.data = unquote_plus(self.data) else: self.data = urlencode(parse_qsl( self.data.replace("+", "%2B"), 1), "POST") try: headers["User-agent"] = self.user_agent if header: headers.update(dict([tuple(_.split("=", 1)) for _ in self.headers.split("\\n")])) if self.verbose >= 2: print("%s REQUEST\nURL: " "%s\n%sHeaders: %s\n" % (DEBUG, self.url, "DATA: %s\n" % self.data if data else "", headers)) req = Request(self.url, self.data if data else None, headers) conn = urlopen(req) except KeyboardInterrupt: raise except HTTPError as error: conn = error if(self.valid_http_status and "*" not in self.valid_http_status["value"] and error.code == int(self.valid_http_status["value"])): pass elif(self.valid_http_status and "*" in self.valid_http_status["value"] and str(error.code)[0] == self.valid_http_status["value"][0]): pass if(self.invalid_http_status and "*" not in self.invalid_http_status["value"] and error.code == int(self.invalid_http_status["value"])): invalid = True elif(self.invalid_http_status and "*" in self.invalid_http_status["value"] and str(error.code)[0] == self.invalid_http_status["value"][0]): invalid = True else: if self.verbose: print_http_error(error) except Exception, error: if hasattr(error, "read"): page = error.read() if self.verbose: print_http_error(error)