【发布时间】: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