错误 400:redirect_uri_mismatch - Google 电子表格 API - Python

Error 400: redirect_uri_mismatch - Google Spreadsheet API - Python

我在使用 OAuth 连接到 Google 电子表格时遇到困难。我花了几天时间搜索并尝试自己修复它。现在我找不到任何我做错的事了。

我尝试使用 URI 地址,我尝试更改 OAuth json。我搜索了 stack-overflow,它看起来很明显,但直到现在任何修复都对我不起作用。

这是我在 python 中所做的,如 https://developers.google.com/sheets/api/quickstart/python#step_3_set_up_the_sample

中所述
creds = None

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

if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)
# 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(
            'C:\Users\pavel\Disk Google\finance\nemovitosti\nemovitostiSecretOauth.json', SCOPES)
        creds = flow.run_local_server(port=8000)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

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

# The ID of the spreadsheet to update.
spreadsheet_id = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'  

# Values will be appended after the last row of the table.
range_ = 'A2:BL2'

# How the input data should be interpreted.
value_input_option = 'RAW'

# How the input data should be inserted.
insert_data_option = 'INSERT_ROWS'

value_range_body = {"values": [["a", "b"]], "range": "A1:B1"}

request = service.spreadsheets().values().append(spreadsheetId=spreadsheet_id, range=range_, valueInputOption=value_input_option, insertDataOption=insert_data_option, body=value_range_body)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)

Google 控制台:

json 文件

在您的脚本中,重定向 URI 的创建方式与 redirect_uri=http://localhost:8000/ 类似。在这种情况下,注册的“Authorized redirect URIs”与它不同。这样,就会出现这样的错误。所以这种情况下,请将“Authorized redirect URIs”处的http://localhost:8000修改为http://localhost:8000/如下,再测试一下

注:

  • 而且,在这种情况下,似乎即使删除了“授权 JavaScript 来源”的 http://localhost:8000http://127.0.0.1:8000,授权仍然有效。