gpt4 book ai didi

python - 无法运行谷歌云 "ValueError: Authorized user info was not..."

转载 作者:行者123 更新时间:2023-12-05 05:58:49 24 4
gpt4 key购买 nike

我正在尝试为 Google Drive Python API 运行 quickstart.py,但出现此错误:

Traceback (most recent call last):
raise ValueError(
ValueError: Authorized user info was not in the expected format, missing fields client_id, client_secret, refresh_token.

Process finished with exit code 1

代码:

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# If modifying these scopes, delete the file token.json.
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.json 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.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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.json', 'w') as token:
token.write(creds.to_json())

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

最佳答案

将您的 xx.json 文件编辑为以下格式:

{
"project_id":"xxxxxxxxxxxx",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"client_id":"xxxxxxxxxxxxx",
"client_secret":"xxxxxxxxxxxx",
"auth_provider_x509_cert_url":"xxxxxxxxxxx",
"redirect_uris":["xxxxxxxxxxxxxxxx","xxxxxxxxxx"]
}

关于python - 无法运行谷歌云 "ValueError: Authorized user info was not...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68362879/

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