获取Facebook用户喜欢Python社交认证 [英] Get Facebook user likes in Python Social Auth
问题描述
我正在使用Django开始一个项目,我试图允许用户使用Facebook登录。为了网站的目的,我想将用户喜欢存储在我的数据库中。
I'm starting a project with Django and I'm trying to allow users log in with Facebook. For the site purposes, I want to store the user likes in my database.
我以Python Social Auth为例(我正在使用的图书馆)对于项目),我已经写了一个管道存储数据在数据库中。
I took as an example the example app on Python Social Auth (the library I'm using for the project) and I've written a pipeline for storing data in DB.
当我尝试访问喜欢时,问题出现,因为我得到的响应的数据只是给我基本的数据,所以以下代码不起作用。
The problem comes when I try to access likes, because the data that the response I get just gives me the basic data, so the following code doesn't work.
def user_details(strategy, details, response, user=None, *args, **kwargs):
attrs = {'user': user}
if strategy.backend.__class__.__name__ == 'FacebookOAuth2':
#no response['likes']
for i in response['likes']:
for d in i['data']:
p = UserPreferences(user=user,like=d['name'])
p.save()
我在Facebook上的应用程序要求喜欢的权限,我在settings.py上有以下内容(使用secret和al我的东西):
My app in Facebook asks for likes permissions, and I have the following on my settings.py (with the secret and all that stuff):
SOCIAL_AUTH_SCOPE = ['user_likes']
任何方式使这项工作?
推荐答案
重命名设置 SOCIAL_AUTH_SCOPE
至 SOCIAL_AUTH_FACEBOOK_SCOPE
。您不需要在认证过程中自动发送喜欢的内容,因此您需要将其查询到Facebook API,例如:
Rename the setting SOCIAL_AUTH_SCOPE
to SOCIAL_AUTH_FACEBOOK_SCOPE
. Likes aren't sent automatically in the auth process, you need to query them to Facebook API, for example:
def get_likes(strategy, details, response, *args, **kwargs):
if strategy.backend.name == 'facebook':
likes = strategy.backend.get_json(
'https://graph.facebook.com/%s/likes' % response['id'],
params={'access_token': response['access_token']}
)
for like in likes['data']:
pass # Process and save likes here
这篇关于获取Facebook用户喜欢Python社交认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!