"""
auth with main_2

regarder aussi :
https://github.com/davidedelpapa/gphotospy/blob/master/gphotospy/authorize.py
"""

import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.auth.transport.requests import Request
import logging


def get_credentials(client_secret_file, token_path, scopes):

    cred = None

    if os.path.exists(token_path):
        with open(token_path, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes)
            cred = flow.run_local_server()

        with open(token_path, 'wb') as token:
            pickle.dump(cred, token)

    return cred


def create_service(client_secret_file, api_name, api_version, *scopes):
    """
    Initializes the service, requesting the authorization from the browser.
    If an access token already exists, use it else authentificate from browser

    :param client_secret_file: JSON file containing the secrets for OAuth,
        as created in the Google Cloud Console
    :param api_version:
    :param api_name:

    :returns
    A service object to pass to the Media, Album, or SharedAlbum contructors
    """
    logging.info(client_secret_file, api_name, api_version, scopes, sep='-')

    token_path = f'token_{api_name}_{api_version}.pkl'
    scopes = [scope for scope in scopes[0]]

    credentials = get_credentials(client_secret_file=client_secret_file,
                                  token_path=token_path,
                                  scopes=scopes)
    service_object = {
        "secrets": client_secret_file
    }

    try:
        service = build(api_name, api_version, static_discovery=False, credentials=credentials)
        logging.debug('service created successfully: {}'.format(api_name))
        service_object["service"] = service
        return service_object

    except Exception as e:
        logging.error(e)

    return None

"""
auth with main_2

regarder aussi :
https://github.com/davidedelpapa/gphotospy/blob/master/gphotospy/authorize.py
"""

import pickle
import os
from google_auth_oauthlib.flow import Flow, InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.auth.transport.requests import Request
import logging


def get_credentials(client_secret_file, token_path, scopes):

    cred = None

    if os.path.exists(token_path):
        with open(token_path, 'rb') as token:
            cred = pickle.load(token)

    if not cred or not cred.valid:
        if cred and cred.expired and cred.refresh_token:
            cred.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(client_secret_file, scopes)
            cred = flow.run_local_server()

        with open(token_path, 'wb') as token:
            pickle.dump(cred, token)

    return cred


def create_service(client_secret_file, api_name, api_version, *scopes):
    """
    Initializes the service, requesting the authorization from the browser.
    If an access token already exists, use it else authentificate from browser

    :param client_secret_file: JSON file containing the secrets for OAuth,
        as created in the Google Cloud Console
    :param api_version:
    :param api_name:

    :returns
    A service object to pass to the Media, Album, or SharedAlbum contructors
    """
    logging.info(client_secret_file, api_name, api_version, scopes, sep='-')

    token_path = f'token_{api_name}_{api_version}.pkl'
    scopes = [scope for scope in scopes[0]]

    credentials = get_credentials(client_secret_file=client_secret_file,
                                  token_path=token_path,
                                  scopes=scopes)
    service_object = {
        "secrets": client_secret_file
    }

    try:
        service = build(api_name, api_version, static_discovery=False, credentials=credentials)
        logging.debug('service created successfully: {}'.format(api_name))
        service_object["service"] = service
        return service_object

    except Exception as e:
        logging.error(e)

    return None

auth with main_2  regarder aussi : https://github.com/davidedelpapa/gphotospy/blob/master/gphotospy/authorize.py Initializes the service, requesting the authorization from the browser.     If an access token already exists, use it else authentificate from browser      :param client_secret_file: JSON file containing the secrets for OAuth,         as created in the Google Cloud Console     :param api_version:     :param api_name:      :returns     A service object to pass to the Media, Album, or SharedAlbum contructors