Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_http_client():
"""Uses project credentials in CLIENT_ID_FILE along with requested OAuth2
scopes for authorization, and caches API tokens in TOKEN_STORE_FILE.
"""
store = file.Storage(TOKEN_STORE_FILE)
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets(CLIENT_ID_FILE, SCOPES)
creds = tools.run_flow(flow, store)
return creds.authorize(Http())
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'gmail-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def __init__(self, flags):
self.__flags = flags
# Perform OAuth 2.0 authorization.
project_dir = os.path.join(
os.getenv('HOME'), 'cloud', 'projects', flags.project)
client_secrets = os.path.join(project_dir, 'client_secrets.json')
oauth2_storage = os.path.join(project_dir, 'oauth2.dat')
flow = flow_from_clientsecrets(client_secrets, scope=GCE_SCOPE)
storage = Storage(oauth2_storage)
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, flags)
self.http = credentials.authorize(httplib2.Http())
self.compute = build('compute', API_VERSION)
@app.route('/oauth/', methods = ['POST'])
def login(provider):
#STEP 1 - Parse the auth code
auth_code = request.json.get('auth_code')
print "Step 1 - Complete, received auth code %s" % auth_code
if provider == 'google':
#STEP 2 - Exchange for a token
try:
# Upgrade the authorization code into a credentials object
oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='')
oauth_flow.redirect_uri = 'postmessage'
credentials = oauth_flow.step2_exchange(auth_code)
except FlowExchangeError:
response = make_response(json.dumps('Failed to upgrade the authorization code.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
# Check that the access token is valid.
access_token = credentials.access_token
url = ('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s' % access_token)
h = httplib2.Http()
result = json.loads(h.request(url, 'GET')[1])
# If there was an error in the access token info, abort.
if result.get('error') is not None:
response = make_response(json.dumps(result.get('error')), 500)
response.headers['Content-Type'] = 'application/json'
def get_credentials(args):
storage = Storage(AUTH_STORAGE_FILE)
flow = flow_from_clientsecrets(filename=CLIENT_SECRETS,
scope=SCOPES)
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow=flow,
storage=storage,
flags=args,
http=http)
storage.put(credentials)
return credentials
'logging_level'
] )
args.noauth_local_webserver = True
args.logging_level='ERROR'
# how and where tokens are stored
storage = Storage(oauth_file)
# http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.multistore_file-module.html
credentials = storage.get()
if credentials is None or credentials.invalid:
flow = flow_from_clientsecrets( CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,)
# do the "allow access" step, save token.
credentials = run_flow(flow, storage, args)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("youtube-api-snippets-oauth2.json")
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
# Trusted testers can download this discovery document from the developers page
# and it should be in the same directory with the code.
return build(API_SERVICE_NAME, API_VERSION,
http=credentials.authorize(httplib2.Http()))
shutil.rmtree(temp_folder, ignore_errors=True)
logger.info("Running pdf2text.py " + version)
SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flags= tools.argparser.parse_args([])
#flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
#flags=tools.argparser.parse_args(args=[])
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store, flags)
service = build('drive', 'v3', http=creds.authorize(Http()))
file_metadata = {'name': 'ocr_files_shrini',
'mimeType': 'application/vnd.google-apps.folder'
}
folder = service.files().create(body=file_metadata,
fields='id').execute()
new_folder_id=folder.get('id')
# Read the config file
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print 'Storing credentials to ' + credential_path
return credentials
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, DIRECTORY_NAME, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, CREDENTIALS_JSON)
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials