Google의 Python API를 사용하여 감사 정보를 가져 오지만 argparse (API 액세스에 필요함) 및 자체 인수 (예 : 통과)에 대한 부모 그룹 인수를 가져올 수 없습니다. 날짜에) 함께 작업 할 수 있습니다.Google Admin API와 함께 Argparse 사용
코드 :
import pprint
import sys
import re
import httplib2
import json
import collections
import argparse
from oauth2client import client
from apiclient import sample_tools
from apiclient import discovery
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from oauth2client.tools import run
from oauth2client import tools
def main(argv):
# Parser for command-line arguments.
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[tools.argparser])
parser.add_argument("-d","--selected_date", help="Date (YYYY-mm-dd) to run user usage report", required=True)
args = parser.parse_args(argv[1:])
print args
selected_date = args.selected_date
print selected_date
# Authenticate and construct service.
service, flags = sample_tools.init(
argv, 'admin', 'reports_v1', __doc__, __file__,
scope='https://www.googleapis.com/auth/admin.reports.usage.readonly')
# If the Credentials don't exist or are invalid run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('admin.dat')
credentials = storage.get()
if not credentials or credentials.invalid:
credentials = run(FLOW, storage)
그리고 명령 줄에서 실행 ...
> python user_report.py
usage: user_report.py [-h] [--auth_host_name AUTH_HOST_NAME]
[--noauth_local_webserver]
[--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
[--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] -d
SELECTED_DATE
user_report.py: error: argument -d/--selected_date is required
좋은 지금까지 ... 지금
> python user_report.py -d "2014-09-14"
Namespace(auth_host_name='localhost', auth_host_port=[8080, 8090], logging_level='ERROR', noauth_local_webserver=False, selected_date='2014-09-14')
usage: user_report.py [-h] [--auth_host_name AUTH_HOST_NAME]
[--noauth_local_webserver]
[--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
[--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
user_report.py: error: unrecognized arguments: -d 2014-09-14
그것은 나타납니다 인수를 추가 같은데 날짜 인수가 인식되지 않습니다. 어떤 도움을 많이 주시면 감사하겠습니다!
당신은 또한 인수가 필요로 userkey을 포함해야 문제를 해결할? https://developers.google.com/resources/api-libraries/documentation/admin/reports_v1/python/latest/admin_reports_v1.userUsageReport.html pydocs에서 날짜가 더 이상 필요하지 않은 것 같습니다. – Emily
나중에 모든 사용자의 이벤트를 가져 오는 "all"로 하드 코딩 된 app에 userKey를 추가합니다. 이 코드는 외부 인수를 전달하지 않을 때 작동합니다. 감사! – Alex