简体   繁体   English

尝试从Google Drive Api v3获取文件列表时出现编码错误

[英]Encoding error when trying to obtain list of files from Google Drive Api v3

I am trying to learn how to program a web app that queries Google Drive Api. 我正在尝试学习如何编写一个查询Google Drive Api的网络应用程序。 My web is in python-Flask. 我的网站是python-Flask。 I've gone through most details successfully but I'm stuck here: 我成功地完成了大部分细节,但我被困在这里:

@app.route('/analyze')
def analyze():
    if 'credentials' not in session:
        return redirect('authorize')
    credentials = google.oauth2.credentials.Credentials(session['credentials'])

drive_service = googleapiclient.discovery.build('drive', 'v3', credentials=credentials)
    files = drive_service.files().list().execute()

The credentials are loaded from the session (the user goes through the authorization flow in a different page / script). 从会话加载凭证(用户在不同的页面/脚本中通过授权流程)。 On this script, we recover the session and make a call to the Google Api: 在此脚本中,我们恢复会话并拨打Google Api电话:

But the call drive.files().list().execute() fails 但调用drive.files().list().execute()失败

The error is in the API's response and is as follows: 错误在API的响应中,如下所示:

ValueError: {'client_id': 'heregoesclientid', 'client_secret': 'thisisclientsecret', 'refresh_token': 'xxxxxxxxxxxxxxxxxx', 'scopes': [' https://www.googleapis.com/auth/drive '], 'token': 'xxxxxxxxxxxxxxxxxxxxxxxx', 'token_uri': ' https://oauth2.googleapis.com/token '} could not be converted to unicode ValueError:{'client_id':'heregoesclientid','client_secret':'thisisclientsecret','refresh_token':'xxxxxxxxxxxxxxxxxx','scopes':[' https://www.googleapis.com/auth/drive '],'令牌':'xxxxxxxxxxxxxxxxxxxxxxxx','token_uri':' https ://oauth2.googleapis.com/token'}无法转换为unicode

Why can't the response be converted to Unicode? 为什么不能将响应转换为Unicode? Is this a bug in Google API? 这是Google API中的错误吗? Is there anything I could do to solve this? 有什么办法可以解决这个问题吗?

If you're using the google-auth package, you need to pass the values into Credentials individually, instead of a dict: See here . 如果您使用的是google-auth软件包,则需要单独将值传递给Credentials ,而不是dict: 请参阅此处

Your code may then look like: 您的代码可能如下所示:

    sc = session['credentials']
    credentials = google.oauth2.credentials.Credentials(
        token=sc.get('token'),
        client_id=sc.get('client_id'),
        ...etc

    )

Hope this helps, or let me know if you're not using google-auth , as this will be irrelevant. 希望这会有所帮助,或者如果您不使用google-auth ,请告诉我,因为这将无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Drive API v3 Python files()。list()中缺少某些文件 - Certain files missing in Google Drive API v3 Python files().list() Google Drive API v3:service.files().list 不返回所有文件夹 - Google Drive API v3: service.files().list not returning all folders 使用Google Drive API v3获取文件的大小 - Get size of files with Google Drive API v3 Google drive API V3 从快捷方式获取原始文件 - Google drive API V3 Fetch original file from shortcut Google Drive API v3 files.export 方法抛出 403 错误:“Export 仅支持 Docs Editors 文件。” - Google Drive API v3 files.export method throws a 403 error: “Export only supports Docs Editors files.” Google API Drive V3 检索使用的驱动器存储空间 - Google API Drive V3 retrieving drive storage space used 如何使用 Python 和 Drive API v3 从 Google Drive 下载文件 - How to download a file from Google Drive using Python and the Drive API v3 使用 python,我无法从 Google Drive API v3 访问共享驱动器文件夹 - Using python, I can't access shared drive folders from Google Drive API v3 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python 无法与共享驱动器和文件交互 | Python 谷歌驱动器 API v3 - Cannot Interact with Shared Drives & Files | Python Google Drive API v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM