[Python] python备份文件到Google Drive网盘 →→→→→进入此内容的聊天室

来自 , 2019-07-27, 写在 Python, 查看 94 次.
URL http://www.code666.cn/view/b02f0c43
  1. #coding=utf-8
  2. import httplib2
  3. import pprint
  4.  
  5. from apiclient.discovery import build
  6. from apiclient.http import MediaFileUpload
  7. from oauth2client.client import OAuth2WebServerFlow
  8.  
  9.  
  10. # Copy your credentials from the APIs Console
  11. CLIENT_ID = ''
  12. CLIENT_SECRET = ''
  13.  
  14. # Check https://developers.google.com/drive/scopes for all available scopes
  15. OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
  16.  
  17. # Redirect URI for installed apps
  18. REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
  19.  
  20. # Path to the file to upload
  21. FILENAME = 'document.txt'
  22.  
  23. # Run through the OAuth flow and retrieve credentials
  24. flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
  25. authorize_url = flow.step1_get_authorize_url()
  26. print 'Go to the following link in your browser: ' + authorize_url
  27. code = raw_input('Enter verification code: ').strip()
  28. credentials = flow.step2_exchange(code)
  29.  
  30. # Create an httplib2.Http object and authorize it with our credentials
  31. http = httplib2.Http()
  32. http = credentials.authorize(http)
  33.  
  34. drive_service = build('drive', 'v2', http=http)
  35.  
  36. # Insert a file
  37. media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
  38. body = {
  39.   'title': 'My Documents',
  40.   'description': 'A test document',
  41.   'mimeType': 'text/plain'
  42. }
  43.  
  44. file = drive_service.files().insert(body=body, media_body=media_body).execute()
  45. pprint.pprint(file)
  46.  
  47.  
  48. #//python/8751

回复 "python备份文件到Google Drive网盘"

这儿你可以回复上面这条便签

captcha