由于UnicodeDecodeError无法获得JSON响应? [英] Not able to get JSON response due to UnicodeDecodeError?
问题描述
当我尝试运行python core-backup.py
文件时,我无法获得JSON响应:在代码FB_SHORT_ACCESS_TOKEN和FB_LONG_ACCESS_TOKEN相同.
I am not able to get JSON response when I try to run python core-backup.py
file: In code FB_SHORT_ACCESS_TOKEN and FB_LONG_ACCESS_TOKEN are same.
core-backup.py:
core-backup.py :
import os
from os.path import join
import requests
def refresh_short_token():
"""
Refresh short access token
"""
request_url = FB_URL + 'oauth/access_token'
request_payload = {
'grant_type': 'fb_exchange_token',
'client_id': FB_APP_ID,
'client_secret': FB_APP_SECRET,
'fb_exchange_token': FB_SHORT_ACCESS_TOKEN
}
response = REQ_SESSION.get(request_url, params=request_payload).json()
# dotenvfile = find_dotenv()
# load_dotenv(dotenvfile)
# dotenv.set_key(dotenvfile, "FB_LONG_ACCESS_TOKEN", response['access_token'])
FB_LONG_ACCESS_TOKEN = response["access_token"]
# PAYLOAD['access_token'] = dotenv.get_key(dotenvfile, "FB_LONG_ACCESS_TOKEN")
PAYLOAD['access_token'] = FB_LONG_ACCESS_TOKEN
'''
TODO: refresh_long_token()
A function to refresh the long term access token
Current validity: 60 days
'''
def get_feed():
"""
Fetch feed
"""
request_url = FB_URL + LTTK_GROUP_ID + '/feed'
response = REQ_SESSION.get(request_url, params=PAYLOAD)
if response.status_code == 400:
refresh_short_token()
print(response.json())
return response.json()
def main():
"""
Fetch posts from a Facebook group and populate in database
"""
get_feed()
if __name__ == "__main__":
main()
运行core-backup.py后,我在Windows7中收到UnicodeDecodeError 文件.如何解决此问题. 参见屏幕截图以更清楚:
I am getting UnicodeDecodeError in windows7 after running core-backup.py file. How to fix this issue. See screenshot for more clarity:
文件的完整代码可以在这里找到: https://gist.github.com/anonymous/2ab9e023d631a7cc4dad15237104ee34
Entire code of file can be found out here: https://gist.github.com/anonymous/2ab9e023d631a7cc4dad15237104ee34
推荐答案
似乎您的代码页设置为cp437.在运行python脚本之前,请在终端中输入以下行,尝试将python输出设置为utf-8.
It appears that your code page is set to cp437. Try setting python output to utf-8 by entering the following line in your terminal before running your python script.
set PYTHONIOENCODING=UTF-8
python core-backup.py
这篇关于由于UnicodeDecodeError无法获得JSON响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!