tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

__init__.py (1734B)


      1 """Wrapper for Google OAuth2 API."""
      2 import sys
      3 import json
      4 
      5 import googleapiclient.discovery
      6 import oauth2client
      7 import httplib2 
      8 
      9 from youtube_upload import lib
     10 from youtube_upload.auth import console
     11 from youtube_upload.auth import browser
     12 
     13 YOUTUBE_UPLOAD_SCOPE = ["https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube"]
     14 
     15 def _get_credentials_interactively(flow, storage, get_code_callback):
     16     """Return the credentials asking the user."""
     17     flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
     18     authorize_url = flow.step1_get_authorize_url()
     19     code = get_code_callback(authorize_url)
     20     if code:
     21         credential = flow.step2_exchange(code, http=None)
     22         storage.put(credential)
     23         credential.set_store(storage)
     24         return credential
     25 
     26 def _get_credentials(flow, storage, get_code_callback):
     27     """Return the user credentials. If not found, run the interactive flow."""
     28     existing_credentials = storage.get()
     29     if existing_credentials and not existing_credentials.invalid:
     30         return existing_credentials
     31     else:
     32         return _get_credentials_interactively(flow, storage, get_code_callback)
     33 
     34 def get_resource(client_secrets_file, credentials_file, get_code_callback):
     35     """Authenticate and return a googleapiclient.discovery.Resource object."""
     36     get_flow = oauth2client.client.flow_from_clientsecrets
     37     flow = get_flow(client_secrets_file, scope=YOUTUBE_UPLOAD_SCOPE)
     38     storage = oauth2client.file.Storage(credentials_file)
     39     credentials = _get_credentials(flow, storage, get_code_callback)
     40     if credentials:
     41         http = credentials.authorize(httplib2.Http())
     42         return googleapiclient.discovery.build("youtube", "v3", http=http)