How to deal with OAuth2 error when using Google APIs from Python

Refer to the following article, when using BigQuery from Google APIs Client for Python, an error related to OAuth authentication occurred, so I will leave a solution. It seems that it happened because the file permissions could not read the file cacerts.txt, which defines the SSL certificates that can be connected.

Use BigQuery from python. http://qiita.com/shibacow/items/b8f6445058b374291be5

Using OAuth 2.0 for Server to Server Applications https://developers.google.com/api-client-library/python/auth/service-accounts

environment

OSX 10.10.3 Python 2.7.6 httplib2 0.9.1 google-api-python-client 1.4.1

Event

When using the Google APIs Client Library for Python and using the P12 file and using BigQuery, an authentication-related error occurred and the API could not be accessed.

ssl.SSLError: [Errno 185090050] _ssl.c:343: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib

The executed file is as follows.

bq-access.py


import json
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build

SERVICE_ACCOUNT_EMAIL = '**********@developer.gserviceaccount.com'
PROJECT_NUMBER = '**********'
KEYFILE='******************.p12'

with open(KEYFILE) as f:
  private_key = f.read()

credentials = SignedJwtAssertionCredentials(
    SERVICE_ACCOUNT_EMAIL, private_key,
    'https://www.googleapis.com/auth/bigquery')

http = Http()
credentials.authorize(http)

service = build('bigquery', 'v2', http=http)
response = service.datasets().list(projectId='************').execute()

print response

Workaround

The following page was very helpful.

PYTHON OAUTH2 FAILING WITH X509 ERROR http://bretthutley.com/tag/mac-osx/

It was said that there was a problem with the permissions of the file cacerts.txt of http2lib, so when I looked it up, it was certainly not readable unless it was a group called wheel.

> cd /Library/Python/2.7/site-packages/httplib2-0.9.1-py2.7.egg/httplib2/
> ls -l cacerts.txt
-rw-r-----  1 root  wheel  134862  7 13 18:36 cacerts.txt

Therefore, when I changed the file permissions with the chmod command as follows, I was able to access it without any problems.

> sudo chmod 644 cacerts.txt

Recommended Posts

How to deal with OAuth2 error when using Google APIs from Python
How to deal with SessionNotCreatedException when using Selenium
How to deal with SSL error when connecting to S3 with boto of Python
[Python] How to deal with pandas read_html read error
How to deal with UnicodeDecodeError when executing google image download
How to deal with "Type Error: No matching signature found" error when using pandas fillna
How to update Google Sheets from Python
[Python] How to deal with module errors
How to deal with python installation error in pyenv (BUILD FAILED)
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to deal with errors when installing Python and pip with choco
How to deal with errors when hitting pip ②
How to deal with the problem that the current directory moves when Python is executed from Atom
[AWS] How to deal with WordPress "An error occurred when cropping an image."
How to use Service Account OAuth and API with Google API Client for python
How to scrape image data from flickr with python
Error due to conflict between python when using gurobi
Easy way to scrape with python using Google Colab
How to exit when using Python in Terminal (Mac)
How to analyze with Google Colaboratory using Kaggle API
Error when playing with python
How to correctly upgrade the software when building Linux (CentOS) with Vagrant ~ Using the example of upgrading from Python 2.7 to Python 3.6 ~
How to not load images when using PhantomJS with Selenium
How to resolve CSRF Protection when using AngularJS with Django
[Linux] How to deal with garbled characters when viewing files
[AWS] How to deal with "Invalid codepoint" error in CloudSearch
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
How to update FC2 blog etc. using XMLRPC with python
How to install python using anaconda
Python: How to use async with
Using Rstan from Python with PypeR
Create folders from '01' to '12' with python
How to deal with imbalanced data
How to deal with imbalanced data
How to get started with Python
How to deal with DistributionNotFound errors
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to use FTP with Python
How to calculate date with python
How to access wikipedia from python
How to deal with the terminal getting into the pipenv environment without permission when using pipenv with vscode
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
How to not escape Japanese when dealing with json in python
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
How to get followers and followers from python using the Mastodon API
How to deal with errors when installing whitenoise and deploying to Heroku
How to avoid duplication of data when inputting from Python to SQLite.
[Python] Error and solution memo when using venv with pyenv + anaconda
Unable to bind to interface error when using apollo federation with gqlgen
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
From Python to using MeCab (and CaboCha)
How to work with BigQuery in Python
Try using Python with Google Cloud Functions
How to deal with enum compatibility errors
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop