title: python:IOError: [Errno 13] Permission denied. When I tried google-analytics-api sample code
body: <p>I am trying to get data from google-analytics-api, I run the sample code:HelloAnalytics.py(<a href="https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py" rel="nofollow noreferrer">https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py</a>),
  but there are some error:IOError: [Errno 13]
  I had added my service account email address to my GA account
  I am using mac os.</p>
  
  <p>detail error message:</p>
  
  <pre><code>   Traceback (most recent call last):
    File "/Users/mac/Documents/python-f/HelloAnalytics.py", line 82, in &lt;module&gt;
      main()
    File "/Users/mac/Documents/python-f/HelloAnalytics.py", line 77, in main
      analytics = initialize_analyticsreporting()
    File "/Users/mac/Documents/python-f/HelloAnalytics.py", line 34, in initialize_analyticsreporting
      analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
    File "build/bdist.macosx-10.12-intel/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
    File "build/bdist.macosx-10.12-intel/egg/googleapiclient/discovery.py", line 222, in build
    File "build/bdist.macosx-10.12-intel/egg/googleapiclient/discovery.py", line 269, in _retrieve_discovery_doc
    File "build/bdist.macosx-10.12-intel/egg/oauth2client/transport.py", line 159, in new_request
    File "build/bdist.macosx-10.12-intel/egg/oauth2client/client.py", line 744, in _refresh
    File "build/bdist.macosx-10.12-intel/egg/oauth2client/client.py", line 775, in _do_refresh_request
    File "build/bdist.macosx-10.12-intel/egg/oauth2client/transport.py", line 282, in request
    File "/Library/Python/2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1609, 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.9.2-py2.7.egg/httplib2/__init__.py", line 1351, in _request
      (response, content) = self._conn_request(conn, request_uri, method, body, headers)
    File "/Library/Python/2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1272, in _conn_request
      conn.connect()
    File "/Library/Python/2.7/site-packages/httplib2-0.9.2-py2.7.egg/httplib2/__init__.py", line 1036, in connect
      self.disable_ssl_certificate_validation, self.ca_certs)
    File "/Library/Python/2.7/site-packages/httplib2-0.9.2-py2.7.egg/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 911, in wrap_socket
      ciphers=ciphers)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 520, in __init__
      self._context.load_verify_locations(ca_certs)
  IOError: [Errno 13] Permission denied
  </code></pre>
  
  <p>the source code:</p>
  
  <pre><code>"""Hello Analytics Reporting API V4."""
  
  import argparse
  
  from apiclient.discovery import build
  from oauth2client.service_account import ServiceAccountCredentials
  
  import httplib2
  from oauth2client import client
  from oauth2client import file
  from oauth2client import tools
  
  
  SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
  DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
  KEY_FILE_LOCATION = '/Users/mac/Documents/python-f/MyProject-a7c00ebc9685.p12'
  SERVICE_ACCOUNT_EMAIL = 'data-510@prime-imagery-150409.iam.gserviceaccount.com'
  VIEW_ID = '120100451'
  
  
  def initialize_analyticsreporting():
    """Initializes an analyticsreporting service object.
  
    Returns:
      analytics an authorized analyticsreporting service object.
    """
  
    credentials = ServiceAccountCredentials.from_p12_keyfile(
      SERVICE_ACCOUNT_EMAIL, KEY_FILE_LOCATION, scopes=SCOPES)
  
    http = credentials.authorize(httplib2.Http())
  
    # Build the service object.
    analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
  
    return analytics
  
  
  def get_report(analytics):
    # Use the Analytics Service Object to query the Analytics Reporting API V4.
    return analytics.reports().batchGet(
        body={
          'reportRequests': [
          {
            'viewId': VIEW_ID,
            'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
            'metrics': [{'expression': 'ga:sessions'}]
          }]
        }
    ).execute()
  
  
  def print_response(response):
    """Parses and prints the Analytics Reporting API V4 response"""
  
    for report in response.get('reports', []):
      columnHeader = report.get('columnHeader', {})
      dimensionHeaders = columnHeader.get('dimensions', [])
      metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
      rows = report.get('data', {}).get('rows', [])
  
      for row in rows:
        dimensions = row.get('dimensions', [])
        dateRangeValues = row.get('metrics', [])
  
        for header, dimension in zip(dimensionHeaders, dimensions):
          print header + ': ' + dimension
  
        for i, values in enumerate(dateRangeValues):
          print 'Date range (' + str(i) + ')'
          for metricHeader, value in zip(metricHeaders, values.get('values')):
            print metricHeader.get('name') + ': ' + value
  
  
  def main():
  
    analytics = initialize_analyticsreporting()
    response = get_report(analytics)
    print_response(response)
  
  if __name__ == '__main__':
    main()
  </code></pre>