【问题标题】:RefreshError when trying to connect with gspread尝试与 gspread 连接时出现 RefreshError
【发布时间】:2022-10-04 23:32:38
【问题描述】:

我让 gspread 正常工作了一周,突然尝试打开电子表格我收到以下错误:

RefreshError: ('invalid_client: Unauthorized', {'error': 'invalid_client', 'error_description': 'Unauthorized'})

这是在简单的使用下,如下所示:

gc = gspread.oauth(credentials_filename='credentials.json')
master="filename"
master_file_sh = gc.open(master)

使用谷歌的方法,身份验证工作正常,甚至多次尝试重置凭据都没有运气。

知道为什么它可能会失败吗?我知道该消息清楚地说明了原因,但事实并非如此。

欢迎任何建议。

【问题讨论】:

    标签: gspread


    【解决方案1】:

    我已经设法使用不同的凭据解决了这个问题,而不是像下面这样真正实现 gspread 后端:

    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    
    import os
    
    creds = None
    
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', scope)
    
    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', scope)
            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())
    
    gc=gspread.authorize(creds)
    

    【讨论】:

      猜你喜欢
      • 2016-05-07
      • 2023-03-12
      • 2012-04-20
      • 1970-01-01
      • 2019-10-06
      • 2021-09-18
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多