Skip to content

Commit

Permalink
Switching to sheets api service account
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomad7 committed Jun 23, 2022
1 parent a7402a6 commit 0bf9442
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions TFCmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
import random
# import pickle

from discord.ext import commands
from dotenv import load_dotenv
Expand All @@ -12,33 +13,34 @@
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.auth.exceptions import RefreshError
from google.oauth2 import service_account


# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

# The ID and range of Neon's sheet of maps + team sizes.
SAMPLE_SPREADSHEET_ID = '1sXUDGC9XrRqKNbjq-qZeMNIgQDeRXdimEY1PZmXt6cY'
SAMPLE_RANGE_NAME = 'Full List!A3:D'
secret_file = os.path.join(os.getcwd(), 'SvcAcctCredentials.json')

def pullmaps(players='2'):
pulledmaps = [] # i don't think this is necessary
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
# the file SvcAcctCredentials.json stores service account credentials
# the spreadsheet must be shared with this service account
if os.path.exists('SvcAcctCredentials.json'):
creds = service_account.Credentials.from_service_account_file(secret_file, scopes=SCOPES)

# if service account isn't set up, try prompting the user
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# # Save the credentials for the next run - N/A with service account
# with open('token.json', 'w') as token:
# token.write(creds.to_json())

#check playercount so we can get the right maps
match players:
Expand Down Expand Up @@ -84,12 +86,8 @@ def pullmaps(players='2'):
except HttpError as err:
print(err)

#print(pulledmaps)
return pulledmaps

#if __name__ == '__main__':
#main()

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

Expand Down

0 comments on commit 0bf9442

Please sign in to comment.