我在Facebook上使用fbconsole进行验证。
import fbconsole
fbconsole.APP_ID = '1234567890'
fbconsole.AUTH_SCOPE = ['publish_stream', 'publish_checkins', 'read_stream', 'offline_access']
fbconsole.authenticate()
newsfeed = fbconsole.get('/me/home', {'fields':'from,name,description,message'})
newsfeedData = newsfeed["data"]
for status in newsfeedData:
print status['from']['name'];
print status['created_time'];
#print status['name']
print status['message']
#print status.encode("utf-8")
print('##############################################################################')
status['from']['name']
显示朋友的名字,但status['message']
显示KeyError。如果'status'
是字典类型并且它有'message'
键,我如何从Facebook打印新闻?同样的错误是在'name'
键上打印。
答案 0 :(得分:0)
有时没有“名称”和“消息”值,在这种情况下,facebook不会在响应中包含它们。这可能是它抛出错误的原因。我想你应该检查密钥是否存在。但说实话,我不知道如何用Python做到这一点。
答案 1 :(得分:0)
您可以使用python的(try,except)块来解决问题。 例如。
try:
print(status['message'])
except(KeyError):
print("This post doesn't contain any message")
continue