这是我的代码的一部分:
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
如果凭据过期,则必须刷新。在Windows上,该部分有效,但在Linux上,我得到一个错误(在最后一个字符串上):
('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}', '{\n "error": "invalid_scope",\n "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., /, o, p, r, s, t, u, v, w, :]}",\n "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}')
答案 0 :(得分:0)
我能够重现该错误,我相信如果您将作用域声明如下,将解决该问题:
"scopes": "https://googleapis.com/auth/drive"
代替
"scopes": "googleapis.com/auth/drive"
它肯定应该双向工作,但不行。
请让我知道这是否可以解决。
答案 1 :(得分:0)
您缺少用于授权请求的适当范围。您可能要考虑先遵循Google的教程。
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
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(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if __name__ == '__main__':
main()
答案 2 :(得分:-1)
我是python初学者,遇到类似问题。我编辑了https://developers.google.com/drive/api/v3/quickstart/python,以便能够创建事件。我将范围更改为:
SCOPES = 'https://www.googleapis.com/auth/calendar'
起初,我的应用程序运行良好,但随后很奇怪。第二天,它崩溃了,我必须删除令牌才能再次运行该应用程序。当我运行它时,我必须再次允许访问(通过打开的浏览器)。然后效果很好,但第二天又不能了;)
问题是令牌在3600秒后过期。这就是奇怪行为的原因。令牌过期后,它会运行代码以刷新现有令牌(creds.refresh(Request())
),但是在Windows上却出现错误:
RefreshError('invalid_scope: Some requested scopes were invalid. {invalid=[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}', '{\n "error": "invalid_scope",\n "error_description": "Some requested scopes were invalid. {invalid\\u003d[a, c, d, e, g, h, i, l, m, ., n, /, o, p, r, s, t, u, w, :]}",\n "error_uri": "http://code.google.com/apis/accounts/docs/OAuth2.html"\n}'),)
,应用崩溃。从错误消息中我感到困惑。
当您删除token.pickle时,它需要再次通过浏览器进行授权,之后该应用程序又可以运行3600s;)很多小时我一直在寻找问题所在。
最后我找到了。应该是:
SCOPES = ['https://www.googleapis.com/auth/calendar']
缺少 [和] 括号!!!这是造成上述错误的原因。
现在它对我有用。
提示:如果要测试应用程序的到期时间和令牌刷新是否有效,则只需将系统时钟更改为将来的某个日期即可。