1

I programmed a small app to send data to google spreadsheets. It worked perfectly fine. I changed nothing on the code and suddenly I got authorisation error from google. I check online and found out that the testing accounts expire after 7 days etc... So I generated new OAuth credentials. Deleted the old ones from folder. Deleted the pickle file. And now I get access denied to application on the google authorisation page.

Error 400: redirect_uri_mismatch

I tried adding the url from the site nothing. Still access denied. I didn't even had one before it worked without it.

Here is my python code.

import pandas as pd
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow, Flow
from google.auth.transport.requests import Request
import os
import pickle

# Google Sheets API scopes
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']

# Google Sheet information
SAMPLE_SPREADSHEET_ID = '1Q1bO2rZR5iuA7hxxxxxxxxxMenKrW16FMzUZsfw'
SAMPLE_RANGE_NAME = 'A1:AA1000'  # Adjust this range as needed

# Function to save it all
def save_to_google_sheets(dataframe,team_100_df, team_200_df, team_totals_df, team_bans_df, spreadsheet_id, range_name):
    creds = None
    # The file token.pickle 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.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)  # Replace with your JSON file
            creds = flow.run_local_server(port=0)
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('sheets', 'v4', credentials=creds)

    values = dataframe.astype(str).values.tolist()
    values_team_100_df = team_100_df.astype(str).values.tolist()
    values_team_200_df = team_200_df.astype(str).values.tolist()
    values_team_totals_df = team_totals_df.astype(str).values.tolist()
    values_team_bans_df = team_bans_df.astype(str).values.tolist()
    

    # Clear existing values in the specified range
    service.spreadsheets().values().clear(
        spreadsheetId=spreadsheet_id,
        range=range_name,
    ).execute()
1
  • you didnt need to delete the oauth credetinals just the pickle file. Did you create an installed credentials or web credentials the first time? Commented Dec 21, 2023 at 16:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.