Mocking python HTTP request with an Oauth1 certificate - python

I'm using the python response library to mock a call with requests, but I get this error:
File "/lib/python3.5/site-packages/requests/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/lib/python3.5/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/lib/python3.5/site-packages/requests/sessions.py", line 474, in request
prep = self.prepare_request(req)
File "/lib/python3.5/site-packages/requests/sessions.py", line 407, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/lib/python3.5/site-packages/requests/models.py", line 306, in prepare
self.prepare_auth(auth, url)
File "/lib/python3.5/site-packages/requests/models.py", line 518, in prepare_auth
r = auth(self)
File "/lib/python3.5/site-packages/requests_oauthlib/oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 150, in get_oauth_signature
sig = self.SIGNATURE_METHODS[self.signature_method](base_string, self)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 505, in sign_rsa_sha1_with_client
return sign_rsa_sha1(base_string, client.rsa_key)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 497, in sign_rsa_sha1
key = _prepare_key_plus(alg, rsa_private_key)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 574, in _prepare_key_plus
return alg.prepare_key(keystr)
File "/lib/python3.5/site-packages/jwt/algorithms.py", line 169, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/multibackend.py", line 312, in load_pem_public_key
return b.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1074, in load_pem_public_key
self._handle_key_loading_error()
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1289, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data
The code I try to mock is the following:
def function_to_test():
oauth = OAuth1(
self.consumer_key,
client_secret=self.consumer_secret,
resource_owner_key=self.oauth_token,
resource_owner_secret=self.oauth_token_secret,
rsa_key=self.rsa_key,
signature_method=self._signature_method
)
return requests.post(url="https://example.com", auth=oauth, cert="/path/to/certificate")
And the testing code:
#responses.activate
def test_token_expired(self):
responses.add(responses.POST, url='https://example.com/',
body='my_expected_result',
status=200)
response = function_to_test()
self.assertEqual(response.content, 'my_expected_result)
The test is failing with the error displayed above, does it mean that the responses mocking has not worked? Is it related to auth/cert parameters in my requests.post? Am I missing something?

I think that your issue is not related to responses library, but to OAuth1 initialisation.
Before even trying to mock the request, OAuth1 fails because he is trying to access the key, which is not probably correctly initialised in your test.
You can either give a fake value for this key, on even mock the OAuth1 object, responses will then be able to mock your call.
We could have expected responses to completely mock the requests call, but it seems that some initialisation still happens before that.

Related

Can't authenticate Twitter credentials using python-twitter module

I'm trying to use the python-twitter to eventually set up a twitter bot.
In IDLE, I've
import twitter
api = twitter.Api(consumer_key=['xxxx '],
consumer_secret=['xxx'],
access_token_key=['x-xxx'],
access_token_secret=['xx'])
then
api.VerifyCredentials()
which gives me a long traceback error as follows. What am I doing wrong?
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
api.VerifyCredentials()
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\twitter\api.py", line 4699, in VerifyCredentials
resp = self._RequestUrl(url, 'GET', data)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\twitter\api.py", line 4992, in _RequestUrl
resp = self._session.get(url, auth=self.__auth, timeout=self._timeout, proxies=self.proxies)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 528, in request
prep = self.prepare_request(req)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 456, in prepare_request
p.prepare(
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 320, in prepare
self.prepare_auth(auth, url)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 551, in prepare_auth
r = auth(self)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\requests_oauthlib\oauth1_auth.py", line 108, in __call__
r.url, headers, _ = self.client.sign(
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 209, in _render
headers = parameters.prepare_headers(
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Users\redye\AppData\Local\Programs\Python\Python39\lib\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 56, in escape
raise ValueError('Only unicode objects are escapable. ' +
ValueError: Only unicode objects are escapable. Got <generator object to_unicode.<locals>.<genexpr> at 0x0000027FCEE7B510> of type <class 'generator'>.
Thanks OctaveL for your help.
Eventually I found this video:
https://www.youtube.com/watch?v=ewq-91-e2fw
and followed it exactly. I was worried about exactly what the difference was in the API key and consumer key (there isn't any). Following the instructions in VSCode it all worked fine.
I suspect I messed up some formatting or spaces somewhere.

Google Cloud API: EOFError: Compressed file ended before the end-of-stream marker was reached

I have some really simple Python code that I'm using to do a GCP Google Vault export. However, about 4 out of ever 5 runs returns an EOF Error. The stacktrace is
Traceback (most recent call last):
File "src/initiate_job.py", line 57, in <module>
results = service.matters().list(pageSize=10).execute()
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/googleapiclient/http.py", line 901, in execute
headers=self.headers,
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/googleapiclient/http.py", line 177, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google_auth_httplib2.py", line 190, in request
self._request, method, uri, request_headers)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google/auth/credentials.py", line 133, in before_request
self.refresh(request)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google/oauth2/service_account.py", line 359, in refresh
access_token, expiry, _ = _client.jwt_grant(request, self._token_uri, assertion)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google/oauth2/_client.py", line 153, in jwt_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google/oauth2/_client.py", line 105, in _token_endpoint_request
response = request(method="POST", url=token_uri, headers=headers, body=body)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/google_auth_httplib2.py", line 117, in __call__
url, method=method, body=body, headers=headers, **kwargs)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/httplib2/__init__.py", line 1994, in request
cachekey,
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/httplib2/__init__.py", line 1651, in _request
conn, request_uri, method, body, headers
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/httplib2/__init__.py", line 1621, in _conn_request
content = _decompressContent(response, content)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/site-packages/httplib2/__init__.py", line 460, in _decompressContent
content = gzip.GzipFile(fileobj=io.BytesIO(new_content)).read()
File "/Users/ipq500/opt/miniconda3/lib/python3.7/gzip.py", line 276, in read
return self._buffer.read(size)
File "/Users/ipq500/opt/miniconda3/lib/python3.7/gzip.py", line 482, in read
raise EOFError("Compressed file ended before the "
EOFError: Compressed file ended before the end-of-stream marker was reached
The code that throws this is just:
service = build('vault', 'v1', credentials=delegated_credentials)
results = service.matters().list(pageSize=10).execute()
I tried to add some defensive programming wherein I simply retry anything that throws this error -- but now I'm hitting rate limits. So, I have to really debug why I'm getting this error.
Any and all help would be appreciated!
Based on this question and this conversation, It seems that the file that you read is damaged and the error of end of the file is expected, In this case I suggest to verify if the credentials exist, take the sample as references and also try to perform a double check before to perform any task, On the other hand, it does the same with the file for the file since it is possible that the writing of the file is not finished, including whether it is being compressed or decompressed

Stashy Python Bit Bucket Time out Issue

I have the following code trying to use stashy, to pull and push to Bit Bucket, Well right now I have code that specifically will just display projects/repositories that are given to my account. Very simple.
import stashy
bitbucket = stashy.connect("https://bitbucket.domain.come:port/projects/asdasd/asd/asdadasd/", "username","password")
print(bitbucket.projects.list())
I keep getting this error without any hang or delay, What could be this issue, I am assuming its client sided as again I am not given any delay, making me assume its specifically my machine or my client specifically.
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/Project/ProjectName/test_no_gui.py", line 3, in <module>
print(bitbucket.projects.list())
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\helpers.py", line 78, in list
return list(self.all())
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\helpers.py", line 44, in paginate
response = self._client.get(url, **kw)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\stashy\client.py", line 59, in get
return requests.get(self.url(resource), auth=(self._username, self._password), verify=self._verify, **kw)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\api.py", line 55, in get
return request('get', url, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\adapters.py", line 174, in send
timeout=timeout
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 417, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 232, in _get_conn
return conn or self._new_conn()
File "C:\Users\User\PycharmProjects\Project\venv\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 547, in _new_conn
strict=self.strict)
TypeError: __init__() got an unexpected keyword argument 'strict'
Looking at other libs/api's turns out stashy is depreciated? Please correct me if I am wrong. Gathered from other research/looking at best method/practice to get into bit bucket. Looking at atlassian-python-api instead, will report back.
Edit: Ended up using REST API, and Requests. All these libs, happened to be out of date.

Firebase get request: TypeError: __init__() got an unexpected keyword argument 'strict'

Trying to get Firebase set up and this code produces the error. I've also tried making the restful call simply using requests and I'm getting the exact same error. I'm using python 3.4. What's going on here?
from firebase import firebase
firebase = firebase.FirebaseApplication('https://testDB-72927.firebaseio.com/', authentication=None)
result = firebase.get('/test', None)
print(result)
Traceback (most recent call last):
File "/Users/Parthenon/Desktop/TestProject/Test.py", line 19, in <module>
firstFunc(mylist)
File "/Users/Parthenon/Desktop/TestProject/Test.py", line 14, in firstFunc
result = firebase.get('/test', None)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/firebase/decorators.py", line 19, in wrapped
return f(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/firebase/firebase.py", line 274, in get
return make_get_request(endpoint, params, headers, connection=connection)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/firebase/decorators.py", line 19, in wrapped
return f(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/firebase/firebase.py", line 38, in make_get_request
response = connection.get(url, params=params, headers=headers, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 310, in get
return self.request('GET', url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/adapters.py", line 174, in send
timeout=timeout
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 417, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 232, in _get_conn
return conn or self._new_conn()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 547, in _new_conn
strict=self.strict)
TypeError: __init__() got an unexpected keyword argument 'strict'
My requests module was not up to date.
sudo pip install requests --upgrade

Bulbs/Neo4j: authentication error when creating Graph object

I'm a newbie and trying to learn Python, Bulbs, Neo4j. I have no idea how to debug this problem that occurs at the very start when I just try to create a Graph object. Here's the traceback:
File "test.py", line 12, in __init__
self.graph = Graph()
File "C:\Python27\lib\site-packages\bulbs\neo4jserver\graph.py", line 55, in __init__
super(Graph, self).__init__(config)
File "C:\Python27\lib\site-packages\bulbs\base\graph.py", line 58, in __init__
self.vertices = self.build_proxy(Vertex)
File "C:\Python27\lib\site-packages\bulbs\base\graph.py", line 124, in build_proxy
return self.factory.build_element_proxy(element_class, index_class)
File "C:\Python27\lib\site-packages\bulbs\factory.py", line 19, in build_element_proxy
primary_index = self.get_index(element_class,index_class,index_name)
File "C:\Python27\lib\site-packages\bulbs\factory.py", line 27, in get_index
index = index_proxy.get_or_create(index_name)
File "C:\Python27\lib\site-packages\bulbs\neo4jserver\index.py", line 87, in get_or_create
resp = self.client.get_or_create_vertex_index(index_name,index_config=config)
File "C:\Python27\lib\site-packages\bulbs\neo4jserver\client.py", line 742, in get_or_create_vertex_index
return self.create_vertex_index(index_name, *args, **kwds)
File "C:\Python27\lib\site-packages\bulbs\neo4jserver\client.py", line 697, in create_vertex_index
resp = self.request.post(path, params)
File "C:\Python27\lib\site-packages\bulbs\rest.py", line 131, in post
return self.request(POST, path, params)
File "C:\Python27\lib\site-packages\bulbs\rest.py", line 184, in request
http_resp = self.http.request(uri, method, body, headers)
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1608, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1359, in _request
for authorization in self._auth_from_challenge(host, request_uri, headers, response, content):
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1243, in _auth_from_challenge
challenges = _parse_www_authenticate(response, 'www-authenticate')
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 317, in _parse_www_authenticate
raise MalformedHeader("WWW-Authenticate")
httplib2.MalformedHeader: WWW-Authenticate
I'm running on Windows 7, if that matters. Can anyone help?
Thanks!
According to https://code.google.com/p/httplib2/issues/detail?id=289 httplib2 throws that error when the service you're accessing sends a 401 and requests authentication.
Since version 2.2 Neo4j has built in authentication which is enabled by default, see http://neo4j.com/docs/stable/security-server.html#security-server-auth.
So you can either supply your username and password to bulbs when creating the connection (don't know details of bulbs, so cannot give a more precise advice here) or you can switch off authentication in the neo4j server by setting
dbms.security.auth_enabled=false
in neo4j-server.properties.

Categories

Resources