繁体   English   中英

使用Django包facepy创建facebook通知:[15](#15)必须使用app access_token调用此方法

[英]Create a facebook notification with Django package facepy : [15] (#15) This method must be called with an app access_token

我正在尝试使用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/上的app access_token时,它说它是一个有效的令牌(我收回了我的APP的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>')

希望能帮到别人

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

此外,由于您使用的是app令牌而不是用户令牌,因此您无法在路径中使用“ 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'更改'我/通知'

暂无
暂无

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

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