我正在尝试访问模块中的秘密JSON文件。
from pathlib import Path
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
def report_generator():
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = Path('./clientSec.json')
VIEW_ID = '123456789'
START_DATE = '2020-10-01'
END_DATE = 'today'
credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
# Build the service object.
analytics = build('analyticsreporting', 'v4', credentials=credentials)
# Calling API's
response = analytics.reports().batchGet(body={
'reportRequests': [{
'viewId': VIEW_ID,
'dateRanges': [{'startDate': START_DATE, 'endDate': END_DATE}],
'metrics': [
{"expression": "ga:pageviews"},
{"expression": "ga:avgSessionDuration"}
], "dimensions": [
{"name": "ga:deviceCategory"}
]
}]}).execute()
这是我的文件夹结构。
最好的方法是在托管应用程序时将它们设置为环境变量。您也可以在开发过程中执行此操作,并且可以使用os库来获取它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句