I'm trying to upload a CSV file to google sheets in python (hopefully to the same sheet and different worksheets every time). I went over all the tutorials and i know that i need to :
Authenticate in google to generate an API key which is stored in
credentials.json
.Use the credentials in the code and it works fine on my machine.
def generate_credentials(self):
'''
Retrieves token and generates credentials
'''
creds = None
flow = InstalledAppFlow.from_client_secrets_file('./Memory_test/credentials.json', SCOPES)
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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.pickle', 'wb') as token:
pickle.dump(creds, token)
if creds == None:
flow = InstalledAppFlow.from_client_secrets_file('./Memory_test/credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
return creds
def test_method(self):
creds = self.generate_credentials()
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print('%s, %s' % (row[0], row[4]))
I've managed to do both of these steps on my machine. The problems is when i want to run the code on another machine without generating a new credentials.json
file.
When i run the code on the new machine whenever the code is running i'm required to login in to my google account through which i generated the api key.
Any way to work around that ?