使用Django软件包Facepy创建Facebook通知:[15](#15)必须使用App Access_Token调



我正在尝试使用facepy&Fandjango,但我经常遇到同样的错误,

@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/上查看app 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

最后,我发现问题在哪里。当我尝试

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>')

希望能帮助某人

创建通知时,应使用 app令牌,而不是用户令牌。因此,不需要 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'

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/notifications'更改'me/notifications'

相关内容

最新更新