问题
I want to create an automated product data import for Google Analytics using the managament api (google analytics api v3) and python.
Now, I am facing the follwing issue: There was an API error : 403 : Forbidden
My oAuth works, because I can pull data from analytics. My account should also have sufficient user permissions, because I have created the same function with App Scripts and it worked (using the same property, account and customdatasourceId).
There must be something wrong with line 29 var daily_upload.
Did someone have similar experiences and hopefully a solution for that?
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 18 11:06:45 2017
@author: brnccc
"""
from __future__ import print_function
import argparse
import sys
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
from apiclient.http import MediaFileUpload
def main(argv):
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'analytics', 'v3', __doc__, __file__,
scope='https://www.googleapis.com/auth/analytics')
try:
media = MediaFileUpload('ga_export.csv',
mimetype='application/octet-stream',
resumable=False)
daily_upload = service.management().uploads().uploadData(
accountId='XXXXXXX',
webPropertyId='UA-XXXXXXX-X',
customDataSourceId='XXXXXXXXXXXXXXXXXXX',
media_body=media).execute()
except TypeError as error:
# Handle errors in constructing a query.
print ('There was an error in constructing your query : %s' % error)
except HttpError as error:
# Handle API errors.
print ('There was an API error : %s : %s' %
(error.resp.status, error.resp.reason))
if __name__ == '__main__':
main(sys.argv)
Edit: SOLVED Replaced oauth with service account authentication
来源:https://stackoverflow.com/questions/47905867/python-google-analytics-management-api-throws-error-403-forbidden