【问题标题】:Using Facepy to request facebook data使用 Facepy 请求 Facebook 数据
【发布时间】:2012-07-14 03:58:10
【问题描述】:

这可能是一个愚蠢的问题,但我已经为这个问题挠头太久了。

我正在尝试使用 django 中的 Facepy/social-auth 从 facebook GraphAPI 请求照片信息。

我的视图有以下代码,但是如何将生成的 json 转换为 python 对象?

instance = UserSocialAuth.objects.filter(user=request.user).filter(provider='facebook')
graph = GraphAPI(instance[0].extra_data['access_token'])
p=graph.get('me/photos')

Facepy 看起来很不错,但是文档充其量是很差,有没有更好的 python facebook sdk 与 social-auth 配合得很好?

感谢所有建议。

【问题讨论】:

    标签: django facebook-graph-api python-2.7


    【解决方案1】:

    Facepy 返回原生 Python 对象,而不是 JSON。

    response = graph.get('me/photos')
    
    for photo in response['data']:
        print photo['source']
    

    【讨论】:

      【解决方案2】:

      你可以使用simplejson的loads函数

      from django.utils import simplejson
      simplejson.loads(args)
      

      反序列化 s(包含 JSON 的 strunicode 实例 文档)到 Python 对象。

      If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding
      other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name
      must be specified. Encodings that are not ASCII based (such as UCS-2)
      are not allowed and should be decoded to ``unicode`` first.
      
      ``object_hook`` is an optional function that will be called with the
      result of any object literal decode (a ``dict``). The return value of
      ``object_hook`` will be used instead of the ``dict``. This feature
      can be used to implement custom decoders (e.g. JSON-RPC class hinting).
      
      ``parse_float``, if specified, will be called with the string
      of every JSON float to be decoded. By default this is equivalent to
      float(num_str). This can be used to use another datatype or parser
      for JSON floats (e.g. decimal.Decimal).
      
      ``parse_int``, if specified, will be called with the string
      of every JSON int to be decoded. By default this is equivalent to
      int(num_str). This can be used to use another datatype or parser
      for JSON integers (e.g. float).
      
      ``parse_constant``, if specified, will be called with one of the
      following strings: -Infinity, Infinity, NaN, null, true, false.
      This can be used to raise an exception if invalid JSON numbers
      are encountered.
      
      To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
      kwarg.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-07
        • 2014-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多