首页 > 解决方案 > Google Drive API 快速入门 UnicodeDecodeError:

问题描述

我尝试按照谷歌驱动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

问题一:介绍中找不到token.json,不知道有没有必要。

问题 2:我可以为错误信息做什么?

任何答案都有帮助,非常感谢。

我的开发环境是python3.7

标签: pythonpython-3.xgoogle-drive-apigoogle-api-client

解决方案


推荐阅读