当我尝试运行python core-backup.py
文件时,我无法获得JSON响应:代码FB_SHORT_ACCESS_TOKEN和FB_LONG_ACCESS_TOKEN相同。
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 文件。如何解决这个问题。 有关更清晰的信息,请参见屏幕截图:
可以在此处找到整个文件代码: https://gist.github.com/anonymous/2ab9e023d631a7cc4dad15237104ee34
答案 0 :(得分:1)
您的代码页似乎设置为cp437。尝试在运行python脚本之前在终端中输入以下行,将python输出设置为utf-8。
set PYTHONIOENCODING=UTF-8
python core-backup.py
答案 1 :(得分:0)
尝试将响应编码更改为UTF-8
:
response.encoding = 'UTF-8'
print(response.json())