[英]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.