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.
Related
I can't understand how to make requests to Binance API.
I have tried:
url='https://eapi.binance.com/eapi/v1/trades'
request_param={'symbol':'BTC-13JAN23-15000-C'}
data=requests.get('url,params=request_param')
print(data.json())
And I'm always getting this Error:
Traceback (most recent call last):
File "D:\МОЕ\Python_script\Request_Binance.py", line 4, in <module>
data=requests.get('url,params=request_param')
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 73, in get
return request("get", url, params=params, **kwargs)
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 573, in request
prep = self.prepare_request(req)
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 484, in prepare_request
p.prepare(
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 368, in prepare
self.prepare_url(url, params)
File "C:\Users\m.frolov.PBG\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 439, in prepare_url
raise MissingSchema(
requests.exceptions.MissingSchema: Invalid URL 'url,params=request_param': No scheme supplied. Perhaps you meant http://url,params=request_param?
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
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.
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
Racking my brain over this. Just signed up here because I was having so much trouble with this. Here is my super simple code:
from jira.client import JIRA
jira_user='username'
jira_pass='password'
jira_server='https://jira-01:8443'
jira_options = {'server': jira_server, 'verify': False}
jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_pass))
When executing, I get this:
Traceback (most recent call last):
File "jira1.py", line 12, in <module>
jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_pass))
File "/usr/lib/python2.6/site-packages/jira/client.py", line 219, in __init__
si = self.server_info()
File "/usr/lib/python2.6/site-packages/jira/client.py", line 1504, in server_info
return self._get_json('serverInfo')
File "/usr/lib/python2.6/site-packages/jira/client.py", line 1894, in _get_json
r = self._session.get(url, params=params)
File "/usr/lib/python2.6/site-packages/jira/resilientsession.py", line 78, in get
return self.__verb('GET', url, **kwargs)
File "/usr/lib/python2.6/site-packages/jira/resilientsession.py", line 63, in __verb
r = method(url, **kwargs)
File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 477, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 465, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.6/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.6/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py", line 341, in _make_request
self._validate_conn(conn)
File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connectionpool.py", line 761, in _validate_conn
conn.connect()
File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/connection.py", line 238, in connect
ssl_version=resolved_ssl_version)
File "/usr/lib/python2.6/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 279, in ssl_wrap_socket
cnx.set_tlsext_host_name(server_hostname)
AttributeError: '_socketobject' object has no attribute 'set_tlsext_host_name'
The same thing happens if I remove 'verify': False
I know I can access the API and this user has permissions because I can hit it correctly with curl and get the right results.
Any help is appreciated.
Resolved by reinstalling python-requests via yum.