from __future__ import print_function
import requests
import base64
import os
from dotenv import load_dotenv
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials
from googleapiclient.discovery import build
from google.oauth2.service_account import Credentials

def get_service():
    # Google Drive APIのスコープ
    SCOPES = ['https://www.googleapis.com/auth/drive.file']
    SERVICE_ACCOUNT_FILE = '/var/www/html/zoom/staging/google_api_service_account_credentiual.json'

    credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    service = build('drive', 'v3', credentials=credentials)
    return service

#infoGoogleアカウントのトークン取得
def fetch_bearer_token():

    #infoGoogleアカウント認証/authtoken取得
    load_dotenv()
    account_id = os.getenv('INFO_ACCOUNT_ID')
    client_id = os.getenv('INFO_CLIENT_ID')
    client_secret = os.getenv('INFO_SECRET_ID')
    
    credentials = base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()
    #print(credentials)
    token_url = f"https://zoom.us/oauth/token?grant_type=account_credentials&account_id={account_id}"
    headers = {
        'Authorization': f'Basic {credentials}',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    
    response = requests.post(token_url, headers=headers)
    
    if response.status_code == 200:
        return response.json()['access_token']
    else:
        raise Exception(f"Error fetching bearer token: {response.text}")
