gpt4 book ai didi

Python:身份验证凭据异常: "Request had invalid authentication credentials. Expected OAuth 2 access token"

转载 作者:行者123 更新时间:2023-12-05 05:13:51 28 4
gpt4 key购买 nike

我编写了获取凭据并连接到 google 工作表的函数。我从 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"
}
}

最佳答案

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

我之前的代码

    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()

关于Python:身份验证凭据异常: "Request had invalid authentication credentials. Expected OAuth 2 access token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133205/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com