0

I'm working on collecting users activity logs from Google Drive's Admin SDK. So far I did a python script and I'm getting some activity results in JSON format. But still, the format itself is not the one I'm looking for and here is an example:

{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',...

As you can see, I'm getting some "u" characters in the beginning of every items and I don't know why.

Actually I want to get the output structured in this way:

{
  "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
            }
          ]
        }
      ]
    }
  ]

}

Is it possible to structure it from the script itself ? (Maybe by using SimpleJson ?)

Here is my python code:

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()

This is my first time using Google API and any help would be appreciated.

Thanks !

Felz
  • 7
  • 1
  • 3
  • check this post regarding the unicode prefix http://stackoverflow.com/questions/13940272/python-json-loads-returns-items-prefixing-with-u – glls May 27 '16 at 03:06

1 Answers1

0

Don't let the unicode prefix scare you:

To get you json formatted, just use json.dumps(arg, indent=2) and the layout will be indented and spaced accordingly.

Ex:

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

Result:

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

for more on python's JSON module here

Community
  • 1
  • 1
glls
  • 2,325
  • 1
  • 22
  • 39