【问题标题】:Create a facebook notification with Django package facepy : [15] (#15) This method must be called with an app access_token使用 Django 包 facepy 创建 facebook 通知:[15] (#15) 必须使用应用程序 access_token 调用此方法
【发布时间】:2012-11-09 14:03:06
【问题描述】:

我正在尝试使用 facepy 和 fandjango 创建一个 facebook 通知,但我经常遇到同样的错误,

@facebook_authorization_required
@csrf_exempt     
def notify_self(request):

     token = request.facebook.user.oauth_token.token #user token
     token_app=facepy.utils.get_application_access_token('APP_ID','APP_SECRET_ID') 
     graph = GraphAPI(token)
     graph.post(
        path = 'me/notifications',
        template = '#Text of the notification',
        href = 'URL',
        access_token= token_app
     )

     return HttpResponse('<script type=\'text/javascript\'>top.location.href = \'URL\';</script>')<code>

当我在https://developers.facebook.com/tools/debug/ 上检查应用程序 access_token 时,它说这是一个有效的令牌(我取回了我的应用程序 ID)

我也试试

graph = GraphAPI(token_app)

但它发送给我:

[2500] 必须使用活动访问令牌来查询有关当前用户的信息。

我的应用程序拥有我需要的所有权限,我搜索了一段时间但没有找到任何帮助,所以我在这里问。

编辑:正确的代码是

@facebook_authorization_required
@csrf_exempt     
def notify_self(request):
   token_app=facepy.utils.get_application_access_token('APP_ID','APP_SECRET_ID') 
   graph = GraphAPI(token_app)
   graph.post(
      path = 'me/notifications',
      template = '#Text of the notification',
      href = 'URL'
   ) 

   return HttpResponse('<script type=\'text/javascript\'>top.location.href = \'URL\'</script>')

感谢joaopsf

【问题讨论】:

    标签: python django notifications fandjango


    【解决方案1】:

    最后我发现问题出在哪里。 当我尝试使用时

    graph = GraphAPI(token_app)

    我的方法很好,唯一要做的就是删除

    access_token=token_app

    在 GraphAPI(token_app) 指令时,令牌已保存,因此无需再次提供。

    正确的代码是:

    @facebook_authorization_required
    @csrf_exempt     
    def notify_self(request):
    
       token = request.facebook.user.oauth_token.token #user token
       token_app=facepy.utils.get_application_access_token('APP_ID','APP_SECRET_ID') 
       graph = GraphAPI(token)
       graph.post(
          path = 'me/notifications',
          template = '#Text of the notification',
          href = 'URL',
          access_token= token_app
       ) 
    
       return HttpResponse('<script type=\'text/javascript\'>top.location.href = \'URL\'</script>')
    

    希望对某人有所帮助

    【讨论】:

    • 这对我不起作用。我找到了另一种可行的解决方案;我将在下面发布它作为答案。此外,您在答案中发布的代码与您在问题中发布的代码相同 - 您可能需要检查并修复答案。
    【解决方案2】:

    创建通知时,您应该使用应用令牌,而不是用户令牌。因此,token = ... 行是不必要的。

    此外,由于您使用的是应用令牌而不是用户令牌,因此您不能在路径中使用“me/...”;您必须指定用户 ID。

    这对我有用:

    @facebook_authorization_required
    @csrf_exempt
    def notify_self(request):
        token_app = facepy.utils.get_application_access_token(
            settings.FACEBOOK_APPLICATION_ID,
            settings.FACEBOOK_APPLICATION_SECRET_KEY
        )
        graph = GraphAPI(token_app)
        graph.post(
            path='%s/notifications' % request.facebook.user.facebook_id,
            template='#Text of the notification',
            href='my targe URL',
        )
        return 'etc'
    

    【讨论】:

      【解决方案3】:

      Jiloko忘记更新代码了,我试了下代码,正确的代码在这里:

      @facebook_authorization_required
      @csrf_exempt     
      def notify_self(request):
         token_app=facepy.utils.get_application_access_token('APP_ID','APP_SECRET_ID') 
         graph = GraphAPI(token_app)
         graph.post(
            path = 'me/notifications',
            template = '#Text of the notification',
            href = 'URL'
         ) 
      
         return HttpResponse('<script type=\'text/javascript\'>top.location.href = \'URL\'</script>')
      

      您可以为“USER_ID/通知”更改“我/通知”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        • 1970-01-01
        • 2012-04-06
        • 2011-08-24
        • 2011-12-11
        • 2012-11-22
        • 2013-02-08
        相关资源
        最近更新 更多