简体   繁体   English

如何使用python读取Facebook图形JSON数据?

[英]How to read facebook graph json data using python?

When I print "user_stream" in the development environment, the full string gets printed. 在开发环境中打印“ user_stream”时,将打印完整的字符串。 However when I view what gets printed in the console in the production environment, the full contents of "user_stream" is not printed, which I believe is leading to an error later on in additional code I have. 但是,当我查看生产环境中在控制台中打印的内容时,不会打印“ user_stream”的全部内容,我相信这会在以后的其他代码中导致错误。

My question is: why aren't all the contents of "user_stream" being printed in the console in the production environment and how do I fix this? 我的问题是:为什么不在生产环境的控制台中打印“ user_stream”的所有内容,我该如何解决?

instance = UserSocialAuth.objects.get(user=request.user, provider='facebook')
token = instance.tokens
user_url = "https://graph.facebook.com/me/friends?access_token=" + token['access_token']

u = urllib.urlopen(user_url);
user_stream = json.loads(u.read())

print user_stream

There is an error in your user_stream somehow. 您的user_stream中有某种错误。 The best way to figure out what it is is to either print the string, or print the error message directly. 弄清楚是什么的最好方法是打印字符串,或直接打印错误消息。 These commands should help you figure out what is going on. 这些命令应该可以帮助您了解发生了什么。

print u.read()
print user_stream['error']

I strongly suspect that your access token isn't valid somehow. 我强烈怀疑您的访问令牌无效。 Look into that as well. 还要研究一下。 An example of what you might see from the Graph API Explorer is this: 您可能从Graph API Explorer中看到的示例如下:

{
  "error": {
    "message": "Malformed access token AAACEdEose0cBAM0CipLFjIDZCysqmGyZCRJ6x4JsdSVkb177lM0UNMWqSYZA9BmBY0h3PbUiIJppQCbDZD",
    "type": "OAuthException",
    "code": 190
  }

Read the message there carefully, and see if you can figure out how to correct it. 请仔细阅读那里的消息,看看是否可以解决该问题。 Also, try printing our your full URL and running it in the Graph API Explorer. 另外,尝试打印我们的完整URL并在Graph API Explorer中运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM