Python paypalrestsdk and SSL errors - python

I am trying to pull PayPal order information using Python and paypalrestsdk.
For some reason I am getting an certificate error that does not make sense to me.
payment_history = paypalrestsdk.Payment.all({"count": 10}, api=PayPal_key.Key_api())
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/resource.py", line 137, in all
response = api.get(url)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/api.py", line 222, in get
return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {})
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/api.py", line 135, in request
http_headers = util.merge_dict(self.headers(refresh_token=refresh_token), headers or {})
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/api.py", line 205, in headers
token_hash = self.get_token_hash(refresh_token=refresh_token)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/api.py", line 98, in get_token_hash
"Accept": "application/json", "User-Agent": self.user_agent
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/paypalrestsdk-1.6.2-py2.7.egg/paypalrestsdk/api.py", line 161, in http_call
response = requests.request(method, url, proxies=self.proxies, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/requests-2.5.0-py2.7.egg/requests/api.py", line 49, in request
response = session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/requests-2.5.0-py2.7.egg/requests/sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/requests-2.5.0-py2.7.egg/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/requests-2.5.0-py2.7.egg/requests/adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno 1] _ssl.c:504: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
I checked the paypal.com certificate:
requests.get('https://paypal.com', verify=True)
and got:
<Response [200]>
Could someone help to resolve this?

The certificate presented by paypal.com is different from that presented by their API endpoints, so your test:
requests.get('https://paypal.com', verify=True)
.. does not reveal the issue you're having when trying to use their APIs.
If you are still having issues, I recommend taking a look at upgrading the underlying version of OpenSSL to ensure you're using a version that supports TLS 1.2 which PayPal's API endpoints require.

Related

Python requests throwing SSLError bad handshake with proxy connection

In Python Requests module, using proxy connection to browse some url
I added proxy ip and port to https_proxy and http_proxy environment variables to route the traffic to proxy
import os
import requests
os.environ["HTTPS_PROXY"] = "proxy1.xx.local:8081"
os.environ["HTTP_PROXY"] = "proxy1xx.local:8081"
H = {"X-Authenticated-User": "bharani#dummy.com"}
url = r"https://google.co.in"
r = requests.get(url,headers=H,verify=False)
print r.status_code
print r.text
Response I'm getting from above code
Traceback (most recent call last):
File "pilot.py", line 356, in <module>
r = requests.get(url,headers=H,verify=False)
File "C:\Python27\lib\site-packages\requests\api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: SysCallError(-1, 'Unexpected EOF')",)
pyopenssl, ndg-httpsclient, pyasn1 are also installed.
Not sure What I'm missing.

SSL Handshake error in Python (_ssl.c:590)

I am trying to get a response using python requests. However I'm facing SSL Handshake error. I tried many solutions posted here, but nothing seems to work. Please help. Thanks in advance
This is the code:
import requests
url = "https://androidappsapk.co/download/com.facebook.katana"
requests.get(url, verify = False)
This is the error that I am getting :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 382, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] sslv3 alert handshake failure (_ssl.c:590)
P.S. - I am using Python 2.7.11
I updated OpenSSL to the latest version and it's working fine now :)

How to fix "bad handshake" SSLErrors when utilizing python requests

I'm trying to get access to the BambooHR API (documentation here), but I receive the following error
params = {
'user': username,
'password': password,
'api_token': api_key}
url = 'https://api.bamboohr.com/api/gateway.php/company/v1/login'
r = requests.get(url, params=params)
Error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 31, in <module>
BambooFunctions().login()
File "/Users/chriscruz/Dropbox/PycharmProjects/082716_r2/Shippy/API/bamboo_api2.py", line 26, in login
r = requests.get(url, params=params, auth=HTTPBasicAuth(api_key, 'api_token'))
File "/Library/Python/2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "/Library/Python/2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Python/2.7/site-packages/requests/sessions.py", line 596, in send
r = adapter.send(request, **kwargs)
File "/Library/Python/2.7/site-packages/requests/adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'SSL23_GET_SERVER_HELLO', 'unknown protocol')],)",)
I'm unsure what this is caused by as I've re-installed OpenSSL, Requests, and not sure how to fix this issue.
You try by setting verify=False, use this option if you are using self-signed certificates.
r = requests.get(url, params=params, verify=False)
More info http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

Verify SSL Certificate in Python on Windows 7 64 bit

I need help in verifying SSL certificate within the company's firewall when trying to access the cloud hosted application https://www.quickbase.com/. The pyquickbase module works perfectly when I run the script from home. Here is my code and traceback for your reference.
### Python script to log into database
import quickbase
client = quickbase.Client(username='JohnDoe',password='OpenQB', database='qb_database',
apptoken='xxxdfdafd', base_url="https://www.quickbase.com")
response = client.list_db_pages(database='qb_database')
print(response)
###
Traceback (most recent call last):
File "D:\Users\User123\Documents\pyfund\Quickbase\QB_api_check.py", line 3, in <module>
apptoken='xxxdfdafd', base_url="https://www.quickbase.com")
File "C:\Python27\lib\site-packages\quickbase.py", line 191, in __init__
self.authenticate()
File "C:\Python27\lib\site-packages\quickbase.py", line 263, in authenticate
required=['ticket', 'userid'], ticket=False)
File "C:\Python27\lib\site-packages\quickbase.py", line 219, in request
request = requests.post(url, data, headers=headers)
File "C:\Python27\lib\site-packages\requests\api.py", line 111, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 57, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 475, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 585, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 477, in send
raise SSLError(e, request=request)
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
Ok, I tried setting up the proxy with OS environment variable and that did not work since the requests.get required that I pass the proxy within the call. I created a proxy Dict as
proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
"ftp" : ftp_proxy
}
The https one was the one that was required in order to receive SSL certificate. I will now have to modify the quickbase and pybase modules that I am using to explicitly include the proxies in the calls.

Sign in to twitter with python

I want to sign in to twitter with the code below and scrape twitter's data then:
import requests
import urllib2
with requests.Session() as c:
url = "https://twitter.com/login"
USER = "hadishamgholi74#gmail.com"
PASS = "52518685251868"
c.get(url)
login_data = {"session[username_or_email]": USER, "session[password]": PASS, "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731", "scribe_log": "", "redirect_after_login": "", "authenticity_token": "4d1c2137136cb297b3e83e382b0026d9213fe731"}
c.post(url,data = login_data,headers={"Referer":"https://twitter.com"})
page = c.get("https://twitter.com")
print page.content
But it rise this error:
Traceback (most recent call last):
File "C:/Users/Mehdi/PycharmProjects/scrap/login1.py", line 9, in <module>
c.get(url)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 473, in get
return self.request('GET', url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: EOF occurred in violation of protocol (_ssl.c:581)
What should i do?
According to this post, there is not support for the https protocol yet requests.exceptions.SSLError: [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

Categories