我按照说明在此网站上安装Google云端硬盘API:https://developers.google.com/drive/api/v3/quickstart/python,但出现问题。
运行代码以设置Drive V3 API时,出现关于UTF-8解码的错误。
这是我运行的代码:
"""
Shows basic usage of the Drive v3 API.
Creates a Drive v3 API service and prints the names and ids of the last 10
files
the user has access to.
"""
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
# 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('{0} ({1})'.format(item['name'], item['id']))
问题是,当我运行此程序时,出现此错误:
C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py:255: UserWarning: Cannot access token.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
File "quickstart.py", line 19, in <module>
creds = tools.run_flow(flow, store)
File "C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\site-packages\oauth2client\tools.py", line 203, in run_flow
ClientRedirectHandler)
File "C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\socketserver.py", line 453, in __init__
self.server_bind()
File "C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\http\server.py", line 138, in server_bind
self.server_name = socket.getfqdn(host)
File "C:\Users\malol\AppData\Local\Programs\Python\Python36\lib\socket.py", line 673, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: invalid continuation byte
首先,我不知道如何获取文件token.json,因为它不在Google指南中。
接下来,我搜索了一种将编解码器从UTF-8切换到latin1的方法,因为根据某些主题,0xe9
是latin1字符,但是我不知道该怎么办以及它是否可以解决此问题
感谢您的帮助。