简体   繁体   中英

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

I'm trying to create a facebook notifications with facepy & fandjango but I'm constantly get the same Error,

@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>

When I Check the app access_token on https://developers.facebook.com/tools/debug/ it said that it's a valid token (I get back my APP's ID)

I also try with

graph = GraphAPI(token_app)

but it send me:

[2500] An active access token must be used to query information about the current user.

my app have all the permission that I need, I search for a while but didn't find any help so I'm asking here.

Edit: The correct Code is

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

thanks to joaopsf

Finally I found where was the issues. when I was trying with

graph = GraphAPI(token_app)

I was on the good way, the only things to do was to delete

access_token= token_app

the token is saved when at the instruction GraphAPI(token_app) so there is no need to give it again.

The correct code is :

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

Hope that will help someone

When creating notifications, you should use the app token , not the user token. So, the token = ... line is not necessary.

In addition, since you are using an app token rather than an user token, you cannot use " me/... " in the path; you must specify the user ID.

This is what worked for me:

@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 Has forgotten to update the code, I tried the code and the correct code is here:

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

you can change 'me/notifications' for 'USER_ID/notifications'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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