[英]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.