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/
Related
Ahoy !
I followed the Documentation (https://developers.google.com/google-apps/calendar/quickstart/python ) for google calendar API and this tutorial (http://www.esologic.com/?p=634 ) to create an alarm clock. The principle is quite simple. I put an event in my calendar, and if it is on the calendar "Raspberry" and that the description is "wake" when the time = the time specified, it plays me my music on the Pi.
Except that when I run my script, it will work for a variable time, and then will crash with a random error.
Traceback (most recent call last):
File "alarmclock.py", line 65, in <module>
calendar_event_query()
File "alarmclock.py", line 37, in calendar_event_query
events = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 722, in execute
body=self.body, headers=self.headers)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 596, in new_request
redirections, connection_type)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1609, 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/__init__.py", line 1351, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1272, in _conn_request
conn.connect()
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1036, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "/usr/local/lib/python2.7/dist-packages/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 891, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 566, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 788, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:581)
or
Traceback (most recent call last):
File "alarmclock.py", line 80, in <module>
calendar_event_query()
File "alarmclock.py", line 52, in calendar_event_query
events = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()
File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 722, in execute
body=self.body, headers=self.headers)
File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 596, in new_request
redirections, connection_type)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1609, 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/__init__.py", line 1351, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1278, in _conn_request
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com
So I do not understand if it is a problem with my script, or if it is directly GoogleAPI that misfire and then crash my script?
Here is my script.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from __future__ import print_function
import httplib2
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
from datetime import datetime
import logging
import time
import random
import os
import argparse
try:
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
CALENDAR_ID = 'MY_CALENDAR_ID#group.calendar.google.com'
CLIENT_SECRET_FILE = 'raspi-wake.json'
SCOPE = 'https://www.googleapis.com/auth/calendar.readonly'
MP3_FOLDER = 'mp3'
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'raspberry-wake.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=SCOPE)
flow.user_agent = 'Raspberry Wake'
if flags:
credentials = tools.run_flow(flow, store, flags)
else:
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def calendar_event_query():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
today = datetime.today()
events = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()
for i, event in enumerate(events['items']):
try:
start = event['start']['dateTime'][:-9]
except KeyError:
start = ''
description = event.get('description', '')
now = today.strftime('%Y-%m-%dT%H:%M')
if start >= now and description.lower() == 'wake':
if start == now:
mp3_files = random.choice(os.listdir(MP3_FOLDER))
command = 'mpg123 \'{}/{}\''.format(MP3_FOLDER, mp3_files)
os.system(command)
time.sleep(60)
while True:
calendar_event_query()
time.sleep(5)
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 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
I translating a pretty big text with google translate and to speed up the process in am calling the split up requests in threads. However, from time to time, the threads throw a SSL handshake error. What is going on?
here is the trace
File "/Users/[PROJECT ROOT]/source/graba/models.py", line 1558, in google_translate_list_strings
service = build('translate', 'v2', developerKey='[MY DEV KEY]')
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/oauth2client/util.py", line 120, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/apiclient/discovery.py", line 193, in build
resp, content = http.request(requested_url)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1588, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1336, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1272, in _conn_request
conn.connect()
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1014, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "/Users/[PROJECT ROOT]/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 80, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 372, in wrap_socket
ciphers=ciphers)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 134, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 296, in do_handshake
self._sslobj.do_handshake()
SSLError: [Errno 8] _ssl.c:503: EOF occurred in violation of protocol
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?