upload_video.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from google_auth_oauthlib.flow import Flow
  2. from colorama import init, Fore
  3. import codecs
  4. SCOPES = ['https://www.googleapis.com/auth/youtube.upload']
  5. API_SERVICE_NAME = 'youtube'
  6. API_VERSION = 'v3'
  7. VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted')
  8. def get_credentials(secrets):
  9. # flow = Flow.from_client_config(
  10. # secrets,
  11. ## scopes=SCOPES,
  12. # redirect_uri='urn:ietf:wg:oauth:2.0:oob')
  13. flow = Flow.from_client_secrets_file(
  14. 'c:\\keys\\.client_secrets.json',
  15. scopes= ['https://www.googleapis.com/auth/youtube.upload'],
  16. # redirect_uri='urn:ietf:wg:oauth:2.0:oob')
  17. # redirect_uri='http://api.ptt.cx')
  18. redirect_uri='https://m.hhh.com.tw/hhh-home-mb/index.html')
  19. # Tell the user to go to the authorization URL.
  20. auth_url, _ = flow.authorization_url(prompt='consent')
  21. init(autoreset=True)
  22. print()
  23. print(Fore.GREEN + 'Please go to this URL: ' + Fore.CYAN + str(auth_url))
  24. # The user will get an authorization code. This code is used to get the
  25. # access token.
  26. print()
  27. code = input(Fore.GREEN + 'Enter the authorization code: ')
  28. flow.fetch_token(code=code)
  29. print()
  30. return flow.credentials
  31. # using flow.authorized_session.
  32. # session = flow.authorized_session()
  33. # print(session.get('https://www.googleapis.com/userinfo/v2/me').json())
  34. tok=get_credentials('c:\\keys\\.client_secrets.json')
  35. print(tok)
  36. print(tok.refresh_token)
  37. print(tok.to_json())
  38. fw=codecs.open('c:\\keys\\storage.json','w','utf-8')
  39. fw.write(tok.to_json())
  40. fw.close()