【问题标题】:Python: authentication credentials Exception: "Request had invalid authentication credentials. Expected OAuth 2 access token"Python:身份验证凭据异常:“请求具有无效的身份验证凭据。预期的 OAuth 2 访问令牌”
【发布时间】:2018-11-03 16:20:35
【问题描述】:

我编写了获取凭据并连接到谷歌表的函数。我从googlesheet 检索数据,一切正常。出于某种原因,有时它会给我一个错误。我搜索了一下,发现有一种方法可以解决它,我在下面更改了我的代码。但仍然是同样的问题。有人可以帮帮我吗?

这是我的代码:

scope = ['https://spreadsheets.google.com/feeds',
     'https://www.googleapis.com/auth/drive']

credentials=ServiceAccountCredentials.from_json_keyfile_name ('xxxx.json' , scope)
gc = gspread.authorize(credentials)

wks = gc.open("Test")


def network_list(request,networkname=''):

gs_client = gspread.authorize(credentials)
sheet=wks.get_worksheet(2)

if credentials.access_token_expired:
      gs_client.login()  # refreshes the token

mylist = sheet.get_all_records()

listofvm=[]
for mydict in mylist:
    # print(mydict)
    for key, value in mydict.items():

        if mydict[key] == networkname:
            # print(mydict['Network']+'  ' + mydict['IP_address'] + ' ' + str(mydict['Total_Intentes']))
            # templist.append({'Build':mydict['Build'], 'VM_Name':mydict['VM_Name']})
            # print(mydict['VM_Name'])
            if mydict['VM_Name'] not in listofvm:
                listofvm.append(mydict['VM_Name'])


return render(request, 'vm_list.html',{'listofvm':listofvm, 'networkname':networkname})

下面是我的错误

 APIError at /
 {
  "error": {
   "code": 401,
    "message": "Request had invalid authentication credentials. 
    Expected OAuth 2 access token, login cookie or other valid 
    authentication credential. See 
    https://developers.google.com/identity/sign-in/web/devconsole- 
    project.",
   "status": "UNAUTHENTICATED"
    }
  }

【问题讨论】:

    标签: python google-sheets


    【解决方案1】:

    好的,伙计们,我找到了答案。我只是在这里发帖,将来可能有人会需要那个。所有课程都教打开谷歌电子表格,然后授权您的凭据,但是当您请求谷歌表时,关键就在这里,您的令牌已更改,这就是为什么在请求数据之前将“打开”命令放在您的凭据之后。我把正确的答案放在这里,我试过了,效果很好。

    我之前的代码

        scope = ['https://spreadsheets.google.com/feeds',
       'https://www.googleapis.com/auth/drive']
    
        credentials=ServiceAccountCredentials.from_json_keyfile_name ('xxxx.json' , scope)
        gc = gspread.authorize(credentials)
    
        wks = gc.open("Test")
    
    
        def network_list(request,networkname=''):
    
            gs_client = gspread.authorize(credentials)
            sheet=wks.get_worksheet(2)
    
            if credentials.access_token_expired:
                gs_client.login()  # refreshes the token
    
            mylist = sheet.get_all_records()
    

    正确答案

        scope = ['https://spreadsheets.google.com/feeds',
        'https://www.googleapis.com/auth/drive']
    
        credentials=ServiceAccountCredentials.from_json_keyfile_name ('xxxx.json' , scope)
         gc = gspread.authorize(credentials)
    
    
    
    
         def network_list(request,networkname=''):
    
             gs_client = gspread.authorize(credentials)
             wks = gc.open("Test")
             sheet=wks.get_worksheet(2)
    
             if credentials.access_token_expired:
                 gs_client.login()  # refreshes the token
    
             mylist = sheet.get_all_records()
    

    【讨论】:

      【解决方案2】:

      默认情况下,您会在 1 小时内获得expires 的令牌,这就是您必须再次运行的原因:

      credentials=ServiceAccountCredentials.from_json_keyfile_name ('xxxx.json' , scope)
      gs_client = gspread.authorize(credentials)
      

      或者你也可以试试:gc.login()

      【讨论】:

        猜你喜欢
        • 2019-11-11
        • 2021-03-02
        • 2020-09-11
        • 2019-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-21
        • 2019-09-13
        相关资源
        最近更新 更多