用于vedavapi项目的google api助手
google-services-helper的Python项目详细描述
与所需的google api服务接口的帮助程序包。它是完全独立的通用包。
安装
从pypi安装
pip install google-services-helper
或者从项目根目录运行python setup.py install
示例用法
fromvedavaapi.google_helper.gsheetsimportGSheetsfromvedavaapi.google_helper.gdriveimportGDrivecredentials_file_path='path/to/credentials.json'scopes=['https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/spreadsheets.readonly']#get instance of GSheets helper class, from our credentials.gsheets_helper=GSheets.from_creds_file(credentials_file_path,scopes)#now use it to access data in our required valuesFormat with header row mapping, etc.#we can provide only desired customized parameters in 'pargs' dictionary parameter. if no custom 'fields' are choosen in pargs, then all fields will be returned. if no range mentioned, all sheet values will be returned.vakyas_sheet_values,statuscode=gsheets_helper.sheet_values_for(spreadsheet_id='someGooGleSheetId',sheet_id='Vakyas',pargs={'idType':'title','valuesFormat':'maps','fields':['Vakya_id','Tantrayukti_tag','Vakya'],'range':'1:27'})#get instance of GDrive helper class, from our credentials.gdrive_helper=GDrive.from_creds_file(credentials_file,scopes)#now use it to retrieve spreadsheet files details in a google drive folderlist_of_files_in_folder,status_code=gdrive_helper.list_of_files_in_folder(folder_id='bJUyfjaitxkzsl_jagkkahajru2acd',mime_types=['application/vnd.google-apps.spreadsheet'],additional_pargs={'orderBy':'recency'})
上面的代码只是传递一个credentials.json文件,然后获取一个gsheets/gdrive对象。使用这些helper对象,我们可以使用它们的方法查询工作表/驱动器等。但是,如果我们没有credentials.json文件(它代表所有者对我们的应用程序的授权授权),那么我们必须从我们从google api控制台获得的应用程序的client_secret.json文件中生成一个(这个client_secret.json类似于我们的应用程序的id)。在这种情况下,资源用户和应用程序所有者是相同的。我们必须授予应用程序访问文件的权限。为了下面的休息。一旦credentials.json存在,就不需要这样了。
fromvedavaapi.google_helper.oauthimportAuthorizerauthorizer=Authorizer(client_secret_path='path/to/client_secret.json',credentials_storage_path='path/to/credentials.json',scopes=['https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/spreadsheets.readonly'])authorizer.authorize()#this will redirect us to web browser, and after completion of authorisation credentials will be stored at given path. we can use these credentials and start using helper packages like above.
还有一个factory类,它从单个凭证创建并返回上面的helper对象,并使它们的链接简单得多。
fromvedavaapi.google_helperimportGServices#our factory classgservices=GServices.from_creds_file('/path/to/credentialsfile',scopes=scopes)#instantiate a wrapper factory.#now get required helpers for those credentials from helper factory.gsheets=gservices.gsheets(enable_drive_service_linking=True)#all arguments are optional.gdrive=gservices.gdrive()#then use above helper objects like before.