0

I am trying to retrieve the activity report from my google drive. I have created a project in the developper console and activated the following API: Admin SDK Audit API Drive SDK Drive API

I am using the following script in Python:

import httplib2
import pprint
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow


# Copy your credentials from the APIs Console
CLIENT_ID = '<My_Client_ID>'

CLIENT_SECRET = 'My_Client_Secret'

# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE ='https://www.googleapis.com/auth/admin.reports.audit.readonly'


# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE)
authorize_url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)

headers = {'Authorization': 'Bearer realm=%s' %code}
(resp, content) = http.request(URL,"GET",headers=headers)

It returns that I have an invalid token. I am not sure of what is not working. Is that my http.request or some admin privileges that I do not have?

Thank you for your help!

  • As you mentioned that you are receiving an error as 'Invalid token', so either token might have gone empty or expired. The Credentials object holds refresh and access tokens that authorize access to a single user's data. If there is any issue with the request you will receive a different error message. Please check if the token is valid or not. Hope that helps! – KRR Feb 06 '15 at 23:50

1 Answers1

0

You're building for the Drive API but should be using the Reports API. Try changing the last 3 lines to:

reports_service = build('admin', 'reports_v1', http=http)
result = reports_service.activities().list(
                 applicationName='drive',
                 customerId='my_customer')
print result

note that this does not take into account pagination.

Jay Lee
  • 13,415
  • 3
  • 28
  • 59