Python ValueError: check_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED

2,205 views
Skip to first unread message

Jarad Collier

unread,
Jun 30, 2015, 10:49:48 AM6/30/15
to adwor...@googlegroups.com
I am using Python 3.4.2.  I am attempting to run Adwords API Python 3 examples: adwords_python3_examples_3.4.1\v201502\reporting\download_criteria_report_as_string.py

I have set up my googleads.yaml file here: C:\Users\Owner\googleads.yaml (because it seems to look for it there).

I'm simply trying to fetch an example report (my first one ever) from the API but am running into the below error:

INFO:oauth2client.client:Refreshing access_token
Traceback (most recent call last):
  File "C:\Users\Owner\Downloads\adwords_python3_examples_3.4.1\v201409\reporting\download_criteria_report_as_string.py", line 64, in <module>
    main(adwords_client)
  File "C:\Users\Owner\Downloads\adwords_python3_examples_3.4.1\v201409\reporting\download_criteria_report_as_string.py", line 59, in main
    print(report_downloader.DownloadReportAsString(report))
  File "C:\Python34\lib\site-packages\googleads\adwords.py", line 588, in DownloadReportAsString
    skip_report_header, skip_column_header, skip_report_summary)
  File "C:\Python34\lib\site-packages\googleads\adwords.py", line 754, in _DownloadReportAsStream
    skip_report_summary))
  File "C:\Python34\lib\site-packages\googleads\adwords.py", line 356, in GetReportDownloadHeaders
    headers = self._adwords_client.oauth2_client.CreateHttpHeader()
  File "C:\Python34\lib\site-packages\googleads\oauth2.py", line 150, in CreateHttpHeader
    self.Refresh()
  File "C:\Python34\lib\site-packages\googleads\oauth2.py", line 166, in Refresh
    self.disable_ssl_certificate_validation)))
  File "C:\Python34\lib\site-packages\oauth2client\client.py", line 598, in refresh
    self._refresh(http.request)
  File "C:\Python34\lib\site-packages\oauth2client\client.py", line 769, in _refresh
    self._do_refresh_request(http_request)
  File "C:\Python34\lib\site-packages\oauth2client\client.py", line 800, in _do_refresh_request
    self.token_uri, method='POST', body=body, headers=headers)
  File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 1195, in request
    self.disable_ssl_certificate_validation)
  File "C:\Python34\lib\site-packages\httplib2\__init__.py", line 850, in __init__
    check_hostname=True)
  File "C:\Python34\lib\http\client.py", line 1211, in __init__
    raise ValueError("check_hostname needs a SSL context with "
ValueError: check_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED

I saw this post: Issue 173 regarding httplib2 .  I don't want to blindly change something I have no context to or know the ramifications down the line.

1. My first question is, based on the message above, does it look like my credentials in my yaml file are working? I've never seen what a successful request looks like.

2. What is the most logical way to fix the above ValueError?  What is the primary issue?  Is the best solution to dig around in the httplib2 file and change this line?
check_hostname= disable_ssl_certificate_validation ^ True

Michael Cloonan (AdWords API Team)

unread,
Jun 30, 2015, 1:31:43 PM6/30/15
to adwor...@googlegroups.com, jaradc...@gmail.com
Hello,

I just checked with the maintainer of the Python library, and here's what he had to say: "I do see this issue pop up every so often for users who install the library for the first time. A clean install usually fixes it. I suspect they are picking up an old version of the httplib2 library somehow."


So it seems that the recommendation is to do a clean install, or else to make the modification mentioned in the related issue. It comes down to having an old version of the httplib2.

Regards,
Mike, AdWords API Team

Jarad Collier

unread,
Jun 30, 2015, 10:42:40 PM6/30/15
to adwor...@googlegroups.com, jaradc...@gmail.com
Thanks for that very helpful reply.  I installed the googleads library via yesterday using:
pip install googleads

That installed all the dependencies at that time.

If I do:
pip install httplib2 --upgrade

It returns:
Requirement already up-to-date: httplib2 in c:\python34\lib\site-packages

The last thing I wanted to do was uninstall Python and reinstall it since I have many packages that I found difficult to install initially (pandas, numpy, etc.) on Windows.

The solution that worked was to go to the __init__.py file located at C:\Python34\Lib\site-packages\httplib2 and change this line:

check_hostname=True)

to this:
check_hostname=disable_ssl_certificate_validation ^ True)

And everything now works.  I am able to download reports.  Thank you for your response Michael.
Reply all
Reply to author
Forward
0 new messages