我想使用managament api(google analytics api v3)和python为Google Analytics创建自动化产品数据导入。
现在,我正面临着以下问题: 出现API错误:403:禁止
我的oAuth有效,因为我可以从分析中提取数据。我的帐户也应该有足够的用户权限,因为我已经使用App Scripts创建了相同的功能并且它有效(使用相同的属性,帐户和customdatasourceId)。
第29行var daily_upload肯定有问题。
有人有类似的经历,希望能有解决方案吗?
# -*- 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)
编辑:已解决 用服务帐户身份验证替换了oauth