来自Google云端硬盘管理员SDK activity.list()的JSON输出

时间:2016-05-27 02:39:19

标签: python json google-admin-sdk drive

我正在努力从Google云端硬盘的管理员SDK中收集用户活动日志。到目前为止,我做了一个python脚本,我得到了一些JSON格式的活动结果。但是,格式本身并不是我正在寻找的格式,这是一个例子:

{u'nextPageToken':u'A:6165487685465:-68949849879703739:37156465464:C65dE3qea',u'items':[{u'kind':u'admin#reports#activity',u'actor':{ u'profileId':u'10651651515611643',u'email':u'mail@mail.com'},u'events':[{u'type':u'login',u'name':u'login_success ”,...

正如你所看到的,我在每件物品的开头都会收到一些“你”字符,我不知道为什么。

实际上我想以这种方式获得输出结构:

{
  "kind": "reports#activities",
  "nextPageToken": string,
  "items": [
    {
      "kind": "audit#activity",
      "id": {
        "time": datetime,
        "uniqueQualifier": long,
        "applicationName": string,
        "customerId": string
      },
      "actor": {
        "callerType": string,
        "email": string,
        "profileId": long,
        "key": string
      },
      "ownerDomain": string,
      "ipAddress": string,
      "events": [
        {
          "type": string,
          "name": string,
          "parameters": [
            {
              "name": string,
               "value": string,
               "intValue": long,
              "boolValue": boolean
            }
          ]
        }
      ]
    }
  ]

}

是否可以从脚本本身构建它? (也许是通过使用SimpleJson?)

这是我的python代码:

from __future__ import print_function
import httplib2
import os
import simplejson as json
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Reports API Python Quickstart'


def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
    os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
                               'admin-reports_v1-python-quickstart.json')

store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
    flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
    flow.user_agent = APPLICATION_NAME
    if flags:
        credentials = tools.run_flow(flow, store, flags)
    else:
        credentials = tools.run(flow, store)
    print('Storing credentials to ' + credential_path)
return credentials

def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('admin', 'reports_v1', http=http)

results = service.activities().list(userKey='all', applicationName='drive',
    maxResults=10).execute()
activities = results.get('items', [])

if not activities:
    print('No activities')
else:
    for activity in activities: print(results)

if __name__ == '__main__':
main()

这是我第一次使用Google API,我们将不胜感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

不要让unicode prefix吓到你:

为了让你的json格式化,只需使用json.dumps(arg,indent = 2) 并且布局将相应缩进和间隔。

例如:

import json
print json.dumps({'4': 5, '6': 7}, sort_keys=True,
                  indent=4, separators=(',', ': '))

结果:

{
    "4": 5,
    "6": 7
}

了解有关python的JSON模块here

的更多信息