google-api-python-client: Error: Analytics Reporting API has not been used in project before or it is disabled
Error: Analytics Reporting API has not been used in project before or it is disabled
Environment details
- OS type and version: Ubuntu WSL - 4.19.104-microsoft-standard
- Python version: Python 3.7.6
- pip version: pip 20.0.2 from /home/jonas/anaconda3/lib/python3.7/site-packages/pip (python 3.7)
- google-api-python-client version:
Name: google-api-python-client Version: 1.8.3 Summary: Google API Client Library for Python Home-page: http://github.com/google/google-api-python-client/ Author: Google LLC Author-email: googleapis-packages@google.com License: Apache 2.0 Location: /home/jonas/anaconda3/lib/python3.7/site-packages Requires: google-api-core, httplib2, google-auth-httplib2, six, google-auth, uritemplate
Steps to reproduce
- Google Analytics Reporting API (and also Google Analitycs API) is enabled at cloud console
- Service credential is created and email is added to GA account as a read/analise user
- I´m Using the “Hello Analytics Reporting API V4.” python code
- Using apiclient.discovery and analytics = build(‘analyticsreporting’, ‘v4’, credentials=credentials, cache_discovery=False)
- I also have tryed a workaround bypassing my python code at https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet but when I try to execute the authentication pop-up stays blank and loader keeps spinning forever and chrome dev tools shows
Failed to load resource: net::ERR_QUIC_PROTOCOL_ERROR.QUIC_PACKET_READ_ERROR
Code example
#!/usr/bin/python3.6
"""Hello Analytics Reporting API V4."""
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = '../../gcloud.json'
VIEW_ID = 'xxxxxxx'
def initialize_analyticsreporting():
"""Initializes an Analytics Reporting API V4 service object.
Returns:
An authorized Analytics Reporting API V4 service object.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials, cache_discovery=False)
return analytics
def get_report(analytics):
"""Queries the Analytics Reporting API V4.
Args:
analytics: An authorized Analytics Reporting API V4 service object.
Returns:
The Analytics Reporting API V4 response.
"""
print ("test")
return analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}],
'dimensions': [{'name': 'ga:country'}]
}]
}
).execute()
def print_response(response):
"""Parses and prints the Analytics Reporting API V4 response.
Args:
response: An 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', [])
for row in report.get('data', {}).get('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()
Stack trace
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://analyticsreporting.googleapis.com/v4/reports:batchGet?alt=json returned "Analytics Reporting API has not been used in project xxxx before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=xxxx then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.". Details: "[{'@type': 'type.googleapis.com/google.rpc.Help', 'links': [{'description': 'Google developers console API activation', 'url': 'https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=xxxx'}]}]">
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (6 by maintainers)
Hi @jonaslsl,
I’m going to close this issue as I can’t produce the problem but if you’re still having trouble please re-open it with additional information. Also see @deepaknaikhnvrm’s comment as an additional check.
@Blackrobe Sorry! Let me try to be clear:
Project A have reporting API enabled and also a service credential that is giving me the
Error: Analytics Reporting API has not been used in project before or it is disabled
Few days AFTER I created a project B, with new credentials and reporting API enabled for project B (same process of a new user at Gcloud) and it worked.
Both cases I have followed the “API wizard proccess/flow” that is present in V4 documentation: https://console.developers.google.com/start/api?id=analyticsreporting.googleapis.com&credential=client_key&hl=pt
Also for project A that is not working I have sent all the print screens with matching project-ids, names and etc.
The email from credential A and B is added as a user at GA.
Project B solves my problem, but I am still bugged
@busunkim96 New project and new service account worked. It might be something related with multiple service/oauth accounts or roles inside credentials.