Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python访问Google Drive活动报告_Python_Google Drive Api_Google Admin Sdk - Fatal编程技术网

使用Python访问Google Drive活动报告

使用Python访问Google Drive活动报告,python,google-drive-api,google-admin-sdk,Python,Google Drive Api,Google Admin Sdk,我正在尝试从我的谷歌硬盘中检索活动报告。 我已在开发人员控制台中创建了一个项目,并激活了以下API: 管理SDK 审计API 驱动器SDK 驱动程序接口 我在Python中使用以下脚本: import httplib2 import pprint from apiclient.discovery import build from oauth2client.client import OAuth2WebServerFlow # Copy your credentials from the A

我正在尝试从我的谷歌硬盘中检索活动报告。 我已在开发人员控制台中创建了一个项目,并激活了以下API: 管理SDK 审计API 驱动器SDK 驱动程序接口

我在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)
导入httplib2
导入pprint
从apiclient.discovery导入生成
从oauth2client.client导入OAuth2WebServerFlow
#从API控制台复制您的凭据
客户端ID=“”
CLIENT\u SECRET='My\u CLIENT\u SECRET'
#检查https://developers.google.com/drive/scopes 对于所有可用作用域
OAUTH_示波器https://www.googleapis.com/auth/admin.reports.audit.readonly'
#运行OAuth流并检索凭据
flow=OAuth2WebServerFlow(客户端ID、客户端机密、OAUTH范围)
authorize\u url=flow.step1\u get\u authorize\u url('urn:ietf:wg:oauth:2.0:oob'))
打印“转到浏览器中的以下链接:”+授权\u url
代码=原始输入('输入验证代码:')。条带()
凭证=流程。步骤2_交换(代码)
#创建一个httplib2.Http对象,并使用我们的凭据对其进行授权
http=httplib2.http()
http=凭据。授权(http)
驱动器\服务=build('drive','v2',http=http)
headers={'Authorization':'Bearer realm=%s'%code}
(resp,content)=http.request(URL,“GET”,headers=headers)
它返回我有一个无效的令牌。 我不确定什么不起作用。 这是我的http.request还是我没有的一些管理员权限


谢谢你的帮助

您正在构建驱动器API,但应该使用报告API。尝试将最后3行更改为:

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

请注意,这并没有考虑分页。

正如您提到的,您收到的错误是“无效令牌”,因此任一令牌可能已变空或过期。Credentials对象持有授权访问单个用户数据的刷新和访问令牌。如果请求有任何问题,您将收到不同的错误消息。请检查令牌是否有效。希望有帮助!谢谢你的回答。我想知道你所说的“我的客户”是什么意思。你如何定义它?