pythongoogle-authenticationgoogle-content-api

Connecting to Google Content API via Python


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!


Solution

  • 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/