我搜索并得到了一些有用的链接,以获取gmail化身。我使用social-auth-app-django库并遵循link设置功能。身份验证工作得很好,但仍然停留在获取权限上,我在项目中创建了pipeline.py主配置根,并在我的视图中调用它,就像从TestDjangoAuth.pipeline导入get_avatar的一样。这是我取回神通的正确方式吗?另一个查询是如何在SOCIAL_AUTH_PIPELINE中调用的views.py中使用管道方法,如user_details、get_username。
这是views.py中的重定向方法,给出了一些错误。我想把阿凡达设为会议:
from TestDjangoAuth.pipeline import get_avatar
def social_login(request):
if request.method == 'GET':
if request.user.is_authenticated:
request.session['photo'] = get_avatar()这是我在视图中修改以使用的pipeline.py。
def get_avatar(backend, strategy, details, response, user=None, *args, **kwargs):
url = None
if backend.name == 'google-oauth2':
url = response['image'].get('url')
print(url)
return url当我返回url在我的视图中使用配置文件图片时,会出现以下错误
AttributeError at /auth/complete/google-oauth2/
'str' object has no attribute 'backend'发布于 2019-07-06 06:43:07
通过谷歌搜索和应用一些修改,我终于用下面的代码片段解决了我的问题。
def get_avatar(request, backend, strategy, details, response, user=None, *args, **kwargs):
url = None
# if backend.name == 'facebook':
# url = "http://graph.facebook.com/%s/picture?type=large"%response['id']
# if backend.name == 'twitter':
# url = response.get('profile_image_url', '').replace('_normal','')
if backend.name == 'google-oauth2':
try:
url = response["picture"]
except KeyError:
url = response['image'].get('url')
get_file = download(url)
file_name = url.split('/')[-1]
extension = 'jpeg'
f = BytesIO(get_file)
out = BytesIO()
image = Image.open(f)
image.save(out, extension)
def download(url):
try:
r = requests.get(url)
if not r.status_code == 200:
raise Exception('file request failed with status code: ' + str(r.status_code))
return (r.content)
except Exception as ex:
return ('error')https://stackoverflow.com/questions/56439028
复制相似问题