I have made a Python program that is capable of uploading files into Google Drive. The .py file works perfectly and does not give any errors.
But as I'm going to distribute it for people who do not have Python installed, I must turn the program and the resources into a .exe. I have done this with cx_Freeze. I have used it before and it has always worked.
But now, the Google Drive API seems to be causing errors when the .exe is run. Here are the errors it gives:
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "Test.py", line 41, in <module>
File "C:\Python27\lib\oauth2client\util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python27\lib\oauth2client\client.py", line 1283, in step2_exchange
headers=headers)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, metho
d, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1252, in _conn_request
conn.connect()
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1021, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 80, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "C:\Python27\lib\ssl.py", line 383, in wrap_socket
ciphers=ciphers)
File "C:\Python27\lib\ssl.py", line 141, in __init__
ciphers)
ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate rout
ines:X509_load_cert_crl_file:system lib
It gives this error after the user has typed in the authorization code.
After some debugging, I found out that the line causing this problem was
credentials = flow.step2_exchange(code)
Here is a snippet of the code:
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
webbrowser.open(authorize_url)
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
The CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE and REDIRECT_URI are all configured correctly.
So, what could be the problem here?
If you need to see more code, ask and I will update this question.
Looks like it can't find your SSL certificates. This is kind of understandable because httplib2 loads them from the file system, and nothing tells cx_Freeze to put them in the bundled package.
Read this, it will help: https://github.com/kennethreitz/requests/issues/557#issuecomment-6420819
Related
I am kind of new in Google APIs and I am trying to connect to Google Content API via Python. I am using a service account for that.
My script is quite easy and straightforward:
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
credentials = ServiceAccountCredentials.from_p12_keyfile(
'xxxxx.iam.gserviceaccount.com',
'/Users/Downloads/pkey.txt',
scopes='https://www.googleapis.com/auth/content')
http = credentials.authorize(httplib2.Http())
service = build('https://www.googleapis.com/content', 'v2')
In the authorization process I get this error:
Traceback (most recent call last):
File "/Users/PycharmProjects/authorization.py", line 22, in <module>
service = build('https://www.googleapis.com/content', 'v2')
File "build/bdist.macosx-10.11-intel/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 228, in build
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 275, in _retrieve_discovery_doc
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1659, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1399, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1319, in _conn_request
conn.connect()
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1069, in connect
self.ssl_version, self.host)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 97, in _ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 352, in wrap_socket
_context=self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 579, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 816, in do_handshake
match_hostname(self.getpeercert(), self.server_hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 271, in match_hostname
% (hostname, ', '.join(map(repr, dnsnames))))
ssl.CertificateError: hostname 'https%3a%2f%2fwww.googleapis.com%2fcontent.googleapis.com' doesn't match either of '*.googleapis.com', '*.clients6.google.com', '*.cloudendpointsapis.com', 'cloudendpointsapis.com', 'googleapis.com'
Process finished with exit code 1
Does it have to do with my authorisation? Or I am doing something wrong?
Thanks a lot for your help!
You want to provide build() with the api_name, not a URL.
service = build('content', 'v2')
Source: https://developers.google.com/api-client-library/python/apis/
I am trying to use gspread python module to read the google spreadsheet. But I am getting some error from ssl. I have tried to look into this, but couldn't able to make it work. The gspread module is using python request internally which seems to be use python's ssl package. The code (I have written) and stack trace is provided below with env details.
Env:
python: 2.7.13
Ubuntu: 14.04
openssl: 1.0.1f 6 Jan 2014
Code:
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(user_email, private_key, scope)
google_client = gspread.authorize(credentials)
worksheet = google_client.open_by_url(<worksheet_url>).get_worksheet(1)
list_of_rows = worksheet.get_all_values()
print list_of_rows
Stack trace:
File "<console>", line 1, in <module>
File "/home/nikhil/.venv/nikhil/src/gspread/gspread/models.py", line 344, in get_all_values
cells = self._fetch_cells()
File "/home/nikhil/.venv/nikhil/src/gspread/gspread/models.py", line 231, in _fetch_cells
feed = self.client.get_cells_feed(self)
File "/home/nikhil/.venv/nikhil/src/gspread/gspread/client.py", line 186, in get_cells_feed
r = self.session.get(url)
File "/home/nikhil/.venv/nikhil/src/gspread/gspread/httpsession.py", line 76, in get
return self.request('GET', url, **kwargs)
File "/home/nikhil/.venv/nikhil/src/gspread/gspread/httpsession.py", line 68, in request
response = func(url, data=data, headers=request_headers, verify=False)
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 469, in get
return self.request('GET', url, **kwargs)
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/sessions.py", line 606, in send
r.content
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/models.py", line 724, in content
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/models.py", line 653, in generate
for chunk in self.raw.stream(chunk_size, decode_content=True):
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 256, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "/home/nikhil/.venv/nikhil/lib/python2.7/site-packages/requests/packages/urllib3/response.py", line 186, in read
data = self._fp.read(amt)
File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 573, in read
return self._read_chunked(amt)
File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 642, in _read_chunked
value.append(self._safe_read(chunk_left))
File "/usr/local/lib/python2.7.13/lib/python2.7/httplib.py", line 688, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/usr/local/lib/python2.7.13/lib/python2.7/socket.py", line 384, in read
data = self._sock.recv(left)
File "/usr/local/lib/python2.7.13/lib/python2.7/ssl.py", line 766, in recv
return self.read(buflen)
File "/usr/local/lib/python2.7.13/lib/python2.7/ssl.py", line 653, in read
v = self._sslobj.read(len)
SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:1864)
Any help would be much appreciated.
UPDATE:
I have setup everything from scratch on new machine and also updated the oauth client to utilize new API version. Couldn't make the above env work so keeping this question open for now.
I was on Wifi and it happened to me. I plugged the computer with an ethernet cable and it solved the problem for me.
I am playing around with GMAIL API to create an app to send auto emails from my server. Running my application as a simple user has the following results:
pankgeorg#snf-25181:~/tomotech/gmailer$ python mailer.py
Traceback (most recent call last):
File "mailer.py", line 36, in <module>
gmail_service = build('gmail', 'v1', http=http)
File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/util.py", line 135, in positional_wrapper
File "/usr/local/lib/python2.7/dist-packages/google_api_python_client-1.3.1-py2.7.egg/googleapiclient/discovery.py", line 198, in build
File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/util.py", line 135, in positional_wrapper
File "/usr/local/lib/python2.7/dist-packages/oauth2client-1.4.5-py2.7.egg/oauth2client/client.py", line 547, in new_request
File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1593, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1335, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1257, in _conn_request
conn.connect()
File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 1021, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "/usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg/httplib2/__init__.py", line 80, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "/usr/lib/python2.7/ssl.py", line 886, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 496, in __init__
self._context.load_verify_locations(ca_certs)
IOError: [Errno 13] Permission denied
On the other hand, running with sudo works perfectly
pankgeorg#snf-25181:~/tomotech/gmailer$ sudo python mailer.py
Message Id: 14ad0aea05e*****
To be completely honest, in order to authenicate using --noauth_local_webserver, I run the command with sudo, authenticated and chown the gmail.storage to myself again.
Also, I installed using easy_install because pip install was giving me the following error:
pankgeorg#snf-25181:~/tomotech/gmailer$ sudo pip install --upgrade google_api_python_client
Cleaning up...
Exception:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/usr/local/lib/python2.7/dist-packages/pip/req.py", line 1096, in prepare_files
req_to_install, self.upgrade)
File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 194, in find_requirement
page = self._get_page(main_index_url, req)
File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 568, in _get_page
session=self.session,
File "/usr/local/lib/python2.7/dist-packages/pip/index.py", line 670, in get_page
resp = session.get(url, headers={"Accept": "text/html"})
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 395, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 237, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 506, in send
history = [resp for resp in gen] if allow_redirects else []
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 168, in resolve_redirects
allow_redirects=False,
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/adapters.py", line 330, in send
timeout=timeout
File "/usr/local/lib/python2.7/dist- packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 480, in urlopen
body=body, headers=headers)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 285, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python2.7/httplib.py", line 1001, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 1035, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 997, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 850, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 826, in send
self.sock.sendall(data)
File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", line 323, in sendall
return self.connection.sendall(data)
File "/usr/lib/python2.7/dist-packages/OpenSSL/SSL.py", line 969, in sendall
raise TypeError("buf must be a byte string")
TypeError: buf must be a byte string
Storing debug log for failure in /root/.pip/pip.logrom oath2client import tool
It is my understanding that the root of the problem is the same in both cases.
I also have to note that my laptop (on which I can authenticate normally, on webbrowser, instead of --noauth_local_webserver) it works just fine, even though the installation is done the same way (the problem with pip appears there too).
Thanks in advance and sorry for the long post!
Tutorials I used:
parse arg
code for sending mails
application body is pretty much the quickstart for gmail api.
The httplib2 installer sets incorrect permissions for its httplib2/cacerts.txt file. One solution is to simply make its files readable by anyone by running
chmod o+r -R /usr/local/lib/python2.7/dist-packages/httplib2-0.9-py2.7.egg
However, it may be better to uninstall the version installed by pip, and use your operating system's package manager instead, which probably has the correct permissions for all files. On Debian, this could be accomplished with
pip uninstall httplib2
apt-get install python-httplib2
i wrote a simple code, basically the same as the one on https://developers.google.com/drive/quickstart-python and every time after the first attempt i've the same error :
# Traceback (most recent call last):
# File "C:/plug-ins/googleDrive.py", line 35, in <lambda>
# self.authAction = self.menu.addAction( 'Authentication', lambda: self.runAuthentication() )
# File "C:/plug-ins/googleDrive.py", line 46, in runAuthentication
# credentials = self.flow.step2_exchange( unicode(text) )
# File "D:/Python\oauth2client\util.py", line 120, in positional_wrapper
# return wrapped(*args, **kwargs)
# File "D:/Python\oauth2client\client.py", line 1131, in step2_exchange
# headers=headers)
# File "D:/Python\httplib2\__init__.py", line 1597, in request
# (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
# File "D:/Python\httplib2\__init__.py", line 1345, in _request
# (response, content) = self._conn_request(conn, request_uri, method, body, headers)
# File "D:/Python\httplib2\__init__.py", line 1281, in _conn_request
# conn.connect()
# File "D:/Python\httplib2\__init__.py", line 1013, in connect
# self.disable_ssl_certificate_validation, self.ca_certs)
# File "D:/Python\httplib2\__init__.py", line 80, in _ssl_wrap_socket
# cert_reqs=cert_reqs, ca_certs=ca_certs)
# File "C:\python26\ssl.py", line 350, in wrap_socket
# File "C:\python26\ssl.py", line 118, in __init__
# File "C:\python26\ssl.py", line 293, in do_handshake
# ssl.SSLError: [Errno 8] _ssl.c:480: EOF occurred in violation of protocol
I run this under Python 2.6.4, httplib2 v0.7.7 and google-api-python-client v1.0b9. If someone have an idea...I will be very grateful.
That version of the google-api-python-client (v1.0b9) is beta 9 of the client library, you should upgrade to the release version, which is simply v1.0.
As for the "EOF occurred in violation of protocol", I have only seen that occur on a flaky network connection, is that the case here?
I'm writing a Google AppEngine python application that needs to connect to Google for authentication. I must have changed something which I've spent the last 5 days trying to figure out: this application previously would run (and perform said authentication) while deployed on localhost but fails to do so now. Once I deploy to appengine however, this error ceases to exist. I have established that it isn't an internet connection issue as I've deployed it on another computer and it still gives me the same error.
Here's what the stack trace is like
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
handler.get(*groups)
File "C:\Users\username\Desktop\AppEngine Things\engineapp\main.py", line 372, in get
creds = self.GetCodeCredentials()
File "C:\Users\username\Desktop\AppEngine Things\engineapp\main.py", line 323, in GetCodeCredentials
creds = oauth_flow.step2_exchange(code)
File "lib\oauth2client\client.py", line 883, in step2_exchange
headers=headers)
File "lib\httplib2\__init__.py", line 1445, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "lib\httplib2\__init__.py", line 1197, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "lib\httplib2\__init__.py", line 1167, in _conn_request
response = conn.getresponse()
File "lib\httplib2\__init__.py", line 999, in getresponse
raise httplib.HTTPException()
HTTPException
Running the app via dev_appserver.py on the command line seemed to solve the problem. I really don't have much insight into why that is so though. So now I can't use the GUI launcher for the sdk