使用oauth2更新电子表格并递归上传到google驱动器(支持oauth2、日历、gmail、地理编码、电子表格等)和导入导出google apps脚本源代码
googleDriveAccess的Python项目详细描述
一个访问google驱动器的python工具(oauth2,calendar,gmail, 地理编码、电子表格等)
包装文件 https://github.com/HatsuneMiku/googleDriveAccess/wiki/module_googleDriveAccess
样品
import os import googleDriveAccess as gda # create instance da = gda.DAClient(os.path.abspath('.')) # create parent folders at the same time folderId, folderPath = da.makeDirs('/remote_drive/subfolder_test/subsubfolder') print folderId, folderPath # recursive backup to remote folder da.recursiveUpload('a_local_directory_you_want_to_backup_recursively') # search da.execQuery("explicitlyTrashed=True") da.execQuery("'root' in parents", **{'maxResults': 5}) da.execQuery("'root' in parents and explicitlyTrashed=True", repeattoken=True, **{'maxResults': 500}) # download (change fileId and correct mimeType 'application/octet-stream' etc.) da.downloadFile('/tmp', 'test_document.txt', parentId='root') # OAuth2 oa2 = gda.OAuth2Client(abc=da) ui = oa2.userInfo() act = ui['email'] print act # gmail gm = gda.GmailClient(abc=oa2) mo = gm.sendMsg(act, act, 'message title', 'message text') if mo: mo = gm.modifyLabels(mo['id'], addLabels=['INBOX', 'UNREAD', 'STARRED']) mo = gm.sendMsg(act, act, 'title attach', 'text attach', 'test_document.txt') if mo: mo = gm.modifyLabels(mo['id'], addLabels=['INBOX', 'UNREAD', 'STARRED']) msgs = gm.getMsgEntries(maxResults=3) for msg in msgs['messages']: mo = gm.getMsg(msg['id']) hdrs = gm.getHdrsDict(mo) for k in ('date', 'to', 'from', 'subject'): if k in hdrs: print u'%s: %s' % hdrs[k] # unicode # popup message from calendar may contain u'\xbb' #print u'snippet: %s' % gm.trimWidth(mo['snippet'].replace(u'\xbb', u'>'), 70) print u'snippet: %s' % gm.trimWidth(mo['snippet'], 70) # unicode # calendar import time ca = gda.CalendarClient('Asia/Tokyo', abc=oa2) cals = ca.idList() for cal in cals['items']: print u'%s : %s' % (cal['id'], cal['summary']) # unicode id = cals['items'][0]['id'] print id TEST_TITLE = u'rendez-vous 今日の待ち合わせ' # unicode t = time.time() eo = ca.insertEvent(id, start=ca.isoDate(t), end=ca.isoDate(t + 24 * 3600), # date only location=u'皇居', summary=TEST_TITLE) # unicode eo = ca.insertEvent(id, start=ca.isoTime(t + 1800), end=ca.isoTime(t + 3600), # date and time location=u'京都御所', summary=TEST_TITLE) # unicode # geocoding geo = gda.GeocodingClient('ja', u'日本') print geo.getLatLng(u'福井県敦賀市明神町') print geo.getLocation(35.75, 136.02) geo.ignoreCountryHead = False print geo.getLocation(*geo.getLatLng(u'福井県敦賀市明神町')) print geo.getLatLng(geo.getLocation(35.75, 136.02)) # spreadsheet SHEET_NAME = 'test_spreadsheet_factory' ss = gda.SpreadsheetFactory(abc=oa2)(sheetName=SHEET_NAME) if ss.sheetId is None: ss.createSpreadsheet(SHEET_NAME, csv='c1,c2,c3\n8,32,256\n64,1024,65536\n') print ss.oa2act print ss.sheet()['title'] print ss.sheetId print ss.worksheetId for ws in ss.worksheets(): print u'%s : %s' % (ws.get_worksheet_id(), ws.title.text) for cell in ss.cells(): print u'%s : %s' % (cell.title.text, cell.content.text) # change True when you get a version (2013-07-12) after gdata-2.0.18 # https://code.google.com/p/gdata-python-client/source/list if False: ss.updateCell(1, 1, u'日本語表示') ss.updateCell(3, 3, u'漢字')
如何使用
安装
pip install 'google-api-python-client' pip install googleDriveAccess (or easy_install googleDriveAccess) cd /opt/googleDriveAccess
首先,在google驱动器上创建你的客户id和secret。
在https://console.developers.google.com/project上注册应用程序,然后 '下载客户端id和客户端机密的json'。
将此json文件重命名为“./client\u secret\u[client id].json”。
mv client_secrets.json /opt/googleDriveAccess/client_secret_YOURCLIENTID.json
其次,为客户机id创建缓存文件。
创建“./cicache.txt”文件并将客户机ID写入第一行。
echo YOURCLIENTID > ./cicache.txt
第三,加密秘密文件。
执行./encrypt_client_secret.py以加密下载的json文件。
./encrypt_client_secret.py
检查加密文件是否存在“./client”秘密^ {EM1}$[客户端] id].json.enc'和纯文本json文件'。/client_secret[客户端 id].json'将被删除。
执行./test_upload_first.py以测试OAuth2流和存储 资格证书。
./test_upload_first.py
执行./test_upload_second.py,使用存储的 资格证书。
./test_upload_second.py
执行./test_download_third.py,使用存储的 资格证书。
./test_download_third.py
执行./test_folder_create.py以测试OAuth2并创建文件夹。 执行./test_folder_hierarchy.py测试OAuth2并扫描文件夹。 执行./recursive_upload.py以测试OAuth2并上载文件。
./test_folder_create.py ./test_folder_hierarchy.py ./recursive_upload.py
执行./test_calendar_v3.py以测试OAuth2并添加日历事件。 执行./test_gmail_v1.py测试oauth2并发送邮件和修改 标签。执行./test_geocoding.py来测试地理编码。执行 ./test_spreadsheet_factory.py测试OAuth2和电子表格。
./test_calendar_v3.py ./test_gmail_v1.py ./test_geocoding.py ./test_spreadsheet_factory.py
执行./test_script_prefetch.py以使用查询测试驱动API搜索。
./test_script_prefetch.py
编辑test_script_import_export.py(设置“mode=0”)以测试创建新的 谷歌应用程序脚本'test_google apps script_createCalendarEvent'用于 下面的测试。
执行./test_script_import_export.py以测试create和'get 脚本。
./test_script_import_export.py
编辑测试脚本import export.py(设置'mode=2'和'set 要测试下载的脚本。
执行./test_script_import_export.py以测试下载。
./test_script_import_export.py
编辑下载的脚本 './script_import_export/test_googleappsscript_createCalendarEvent/code.gs' 是的。
编辑test_script_import_export.py(设置“mode=1”)以测试上载。
执行./test_script_import_export.py以测试上载。
./test_script_import_export.py
已知错误
无法创建和更新Google应用程序脚本。
mimeType was changed about specification of uploading Google Apps Script ?
我将刷新缓存.py:
This program will cache each folder (or file) ids assigned by the Google Drive. (Into the cache file cache_folderIds_[Client ID]_[OAuth2Act].sl3 .) Please search and erase a row that has same id from the cache file when you delete your folder or file using another Google Drive client tool.
可能已修复:
When uploading a file that would not be automaticaly handled Google Drive, "Media type 'None' is not supported. Valid media types: [*/*]" error occurred. Because of default mimeType is set to None on uploadFile. So it may correct to catch the exception and retry with 'binary/octet-stream'.
链接
包装文件 https://github.com/HatsuneMiku/googleDriveAccess/wiki/module_googleDriveAccess
您可以在这个github存储库中获得最新版本。 https://github.com/HatsuneMiku/googleDriveAccess
关系
OAuth2Client Gdata桥 https://github.com/hnakamur/gae-oauth2client-spreadsheet
Pytz内存缓存https://github.com/HatsuneMiku/pytz-memcache
pytz memcache(pypi)https://pypi.python.org/pypi/pytz-memcache
许可证
BSD许可证