使用Python访问Google云端硬盘活动报告

时间:2015-02-06 14:15:19

标签: python google-drive-api google-admin-sdk

我正在尝试从我的google驱动器中检索活动报告。 我在开发者控制台中创建了一个项目并激活了以下API: 管理员SDK 审计API 驱动SDK Drive API

我在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)

它返回我有一个无效的令牌。 我不确定什么不起作用。 这是我的http.request或一些我没有的管理员权限吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您正在为Drive API构建,但应使用Reports API。尝试将最后3行更改为:

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

请注意,这并未考虑分页。