我尝试遵循Google驱动器api教程的介绍:developers.google.com/drive/api/v3/quickstart/python,但是当示例文件quickstart.py在下面运行时,它卡住了:
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
def main():
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()))
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()
我得到如下错误:
C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\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 48, in <module>
main()
File "quickstart.py", line 32, in main
creds = tools.run_flow(flow, store)
File "C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\lib\site-packages\oauth2client\tools.py", line 203, in run_flow
ClientRedirectHandler)
File "C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 449, in __init__
self.server_bind()
File "C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\lib\http\server.py", line 139, in server_bind
self.server_name = socket.getfqdn(host)
File "C:\Users\mhlai.TARI\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 676, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte
问题1:我在介绍中找不到token.json,也不知道是否有必要。
问题2:如何处理错误信息?
任何答案都是有帮助的,非常感谢。
我的开发环境在python3.7中