http://blog.csdn.net/btyh17mxy/article/details/9939035 在google apps中域管理员可以访问域中其他用户的数据,包括drive、gmail什么的。当然要想访问就需要进行domain-wide(域)认证。 在google的官方文档中只有https://developers.google.com/drive/del
http://blog.csdn.net/btyh17mxy/article/details/9939035
在google apps中域管理员可以访问域中其他用户的数据,包括drive、gmail什么的。当然要想访问就需要进行domain-wide(域)认证。
在google的官方文档中只有https://developers.google.com/drive/delegation这么一篇,介绍的是drive的domain-wide认证,我参考了这篇文档并做了些修改。
#-coding:utf-8#!/usr/bin/pythonimport httplib2import pprintimport sysfrom apiclient.discovery import buildfrom oauth2client.client import signedjwtassertioncredentialsemail of the service accountservice_account_email = '下图中的email地址'path to the service account's private key fileservice_account_pkcs12_file_path = '密钥文件路径'def getcredentials(user_email): get cardentials for an google apps user args: user_email: the email of the user returns: the cardentials that you can use to get access to the user's data or something f = file(service_account_pkcs12_file_path, 'rb') key = f.read() f.close() credentials = signedjwtassertioncredentials(service_account_email, key, scope='https://www.googleapis.com/auth/drive', sub=user_email) return credentials
这样就获取到了域下指定用户的证书,可以使用该证书构建drive服务,就像下面这样:
#-coding:utf-8#!/usr/bin/pythonimport httplib2from apiclient.discovery import buildfrom apiclient import errorsclass drive: def __init__(self,credentials): self.credentials=credentials def getfilelist(self,maxresults,pagetoken,q,projection=full): http = httplib2.http() http = self.credentials.authorize(http) service = build('drive', 'v2', http=http) result = [] page_token = pagetoken while true: try: #param = {'maxresults':500} param={'maxresults':maxresults,'pagetoken':page_token,'projection':projection,'q':q} if page_token: param['pagetoken'] = page_token files = service.files().list(**param).execute() #print files['items'] result.extend(files['items']) page_token = files.get('nextpagetoken') if not page_token: break except errors.httperror, error: print 'an error occurred: %s' % error break return result def getfilebyid(fileid): get a drive file instance by it's id args: fileid: id of this file item rerurns: a drive file instance if successful, none if otherwise try : file = self.service.files().get(fileid).execute() return file except errors.httperror,e: print 'an error occurred: %s ' % e return none def print_file(file_id): print a file's metadata. args: service: drive api service instance. file_id: id of the file to print metadata for. try: file = self.service.files().get(fileid=file_id).execute() print 'title: %s' % file['title'] print 'mime type: %s' % file['mimetype'] except errors.httperror, error: print 'an error occurred: %s' % error def download_file(drive_file): download a file's content. args: service: drive api service instance. drive_file: drive file instance. returns: file's content if successful, none otherwise. download_url = drive_file.get('downloadurl') if download_url: resp, content = self.service._http.request(download_url) if resp.status == 200: print 'status: %s' % resp return content else: print 'an error occurred: %s' % resp return none else: # the file doesn't have any content stored on drive. return none
#-coding:utf-8#!/usr/bin/pythonimport d_oauthfrom drive import driveif __name__ == '__main__': c = d_oauth.getcredentials('btyh17mxy@gdocsapp.com') drive = drive(c) files = drive.getfilelist(1000,none,none) for f in files: print filename:%s\nfiletype:%s\nfileid:%s\n % (f['title'],f['mimetype'],f['id'])