问题描述
美好的一天。我正在编写一个python程序,该程序从我的Facebook页面请求一些帖子。为此,Facebook提供了一个称为“ Graph API Explorer”的工具。使用类似于GET请求的内容,我可以获得想要的任何东西(允许我访问并拥有有效的令牌)。我已经针对Graph API Explorer提出了自己的解决方案,并且正在生成我的URL。生成URL后,我使用pycurl从Facebook获取包含我所有数据的JSON对象。
当我使用pycurl时,出现以下错误:
pycurl.error: (3,'Illegal characters found in URL')
但是当打印出该URL并将其粘贴到浏览器时,我得到了有效的答复。
URL:https://graph.facebook.com/v7.0/me?fields=posts%7Bmessage%2Cfrom%7D&access_token=<and my access token which is valid>
我的代码如下:
def get_posts_curl(nodes=['posts'],fields=[['message','from']],token_file='Facebook/token.txt'):
curl = pycurl.Curl()
response = BytesIO()
token = get_token_from_file(token_file)
# constructing request.
url = parse_facebook_url_request(nodes,fields,token)
url = convert_to_curl(url)
print("---URL---: " + url)
# curl session and settings.
curl.setopt(curl.CAINFO,certifi.where())
curl.setopt(curl.URL,url)
curl.setopt(curl.WRITEDATA,response)
curl.perform()
curl.close()
return response.getvalue().decode('utf-8')
错误在curl.perform()
弹出
一些可能相关的信息: 前一阵子一切都很好。将程序从运行Windows 10的工作站传输到服务器(Ubuntu 18.04 Server)后,一切运行正常,我将该项目放在一边。直到现在,该错误才会弹出,并且我有一段时间没有接触过该项目了。
解决方法
似乎令牌引起了问题。我已经尝试了约100个令牌,有些导致了问题,有些却没有。另外,解决了所有问题的修复程序是使用urllib3.unquote
from urllib.parse import unquote
...
url = unquote(url)