The third part of the learning notes of a huge API (based on the domain of oAuth2.0

Source: Internet
Author: User
Blog. csdn. netbtyh17mxyarticledetails9939035 in GoogleApps, the domain administrator can access the data of other users in the domain, including Drive and Gmail. Of course, domain-wide (domain) authentication is required for access. Only developers. Google. comdrivedel is available in official google Documents.

Http://blog.csdn.net/btyh17mxy/article/details/9939035 in Google Apps Domain administrators can access the data of other users in the domain, including Drive, Gmail or something. Of course, domain-wide (domain) authentication is required for access. Only https://developers.google.com/drive/del is available in Google's official documentation

Http://blog.csdn.net/btyh17mxy/article/details/9939035

In Google Apps, the domain administrator can access the data of other users in the domain, including Drive and Gmail. Of course, domain-wide (domain) authentication is required for access.

In the official Google Documents, only.

#-Coding: UTF-8 #! /Usr/bin/pythonimport httplib2import pprintimport sysfrom apiclient. discovery import buildfrom oauth2client. client import SignedJwtAssertionCredentials "" Email of the Service Account "SERVICE_ACCOUNT_EMAIL = 'email address '" "Path to the Service Account's Private Key file" "SERVICE_ACCOUNT_PKCS12_FILE_PATH =' key 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

In this way, the certificate of the specified user under the domain is obtained. You can use this certificate to build the Drive service, as shown below:

#-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'])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.