Get profile picture from Facebook using social-auth - django

I am implementing social login in my application and I am already getting normal fields as First Name, Last Name on Facebook and LinkedIn... Now I want to get the profile picture. I did it for LinkedIn and it is working perfectly, i can see the URL on the extra fields from the database.
I am trying to do the same with Facebook, as I saw here https://groups.google.com/forum/?fromgroups#!topic/django-social-auth/Mee8H8HhLQk
defining
FACEBOOK_PROFILE_EXTRA_PARAMS = {'fields': 'picture'}
FACEBOOK_EXTRA_DATA = [('profile', 'profile')]
But when i did this, somethings got messed up. I am not able anymore to get the first name and last name in my user table as I was doing before these two lines of code. Does anyone know why this problem is happening?

The picture is under http://graph.facebook.com/<user_id>/picture. The user_id is given by the FacebookBackend.

Related

Django funcion views, creating a company with user associated

So, i have this problem when im trying to create my "company", when i didnt have user authentication the view worked just fine. Now i've added django allauth, and this screen appears to me:
enter image description here
So i will leave here my model and my view to create the company
Im just a begginner so sry for my bad explanation
enter image description here
enter image description here
The URLS : https://prnt.sc/K-lKvmfuQtvR
It looks like the problem is with the way your url is setup. Do you have a screenshot of the urls.py as well so we can see how you setup your url mapping. (OP added this screenshot: prnt.sc/K-lKvmfuQtvR)
Your companies/<pk>/ endpoint is catching your "create_company" value and storing it as a variable. Try moving your companies/create_company path above your companies/<pk>/ like so
path('companies/', listing_companies),
path('companies/create_company/', company_create),
path('companies/<pk>', retrieve_companies),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
Alternatively rename companies/create_company/ to create_company/ like so
path('companies/', listing_companies),
path('companies/<pk>', retrieve_companies),
path('create_company/', company_create),
path('companies/<pk>/update/', company_update),
path('companies/<pk>/delete/', company_delete),
The end goal in either case is simply to make sure your paths are not capturing your "create_company" and storing it as that variable that it is passing along to your path's method.

Getting profile Linkedin Profile Picture django-allauth

I know that this should be a question, but after such a headache, I thought that I should help some one with this same problem.
I'm using Allauth 0.42.0 and Django 3.0.8, following the allauth documentation, I could not, for the love of my life get the Profile Picture url from the user.
But hours of searching lead me to this solution:
# access_token required by LinkedIn's API
access_token = SocialToken.objects.get(account__user=user, account__provider='linkedin_oauth2')
# The request
r = requests.get(f"https://api.linkedin.com/v2/me?projection=(profilePicture("
f"displayImage~:playableStreams))&oauth2_access_token={access_token}")
# The json on the profile picture key
profile_pic_json = r.json().get('profilePicture')
# There's a lot of pictures sizes, so I put it on a array to easier reading
elements = profile_pic_json['displayImage~']['elements']
# elements[len(elements)-1] returns the pic with the highest resolution
# ['identifiers'][0]['identifier'] is the url itself
url_picture = elements[len(elements)-1]['identifiers'][0]['identifier']
I hope that this help someone.

Field from database showing up as the word "None" on frontend template but exist in the database

I've encountered the weirdest thing while developing a django-python web app.
I have a field in my database that I want to display on the frontend. The field is a MultiSelectField and the list is populated from a list
models.py
# Other service areas
other_areas = MultiSelectField(choices = SERVICE_AREAS, max_length=360,
default=None, null=True)
My SERVICE_AREAS is created by a function like this:
def get_service_areas():
#SERVICE_AREAS = [('None', '-------------')]
SERVICE_AREAS = []
sas = service_area.objects.all()
for s in sas:
SERVICE_AREAS.append((s.name, s.name))
return SERVICE_AREAS
I have a form from where I select the other "service areas" and everything is submitted to my database correctly (in pgadmin I see the correct values)
proof of ok values in DB
Then on the frontend side in my template i have this code to present the data:
index.html
<div class="email" style="padding-top: 10px;">{{c.service_area}}- {{c.service_area.said}} , Other SAID's: {{c.other_areas}}</div>
The weird part about this is that when the data actually show on my screen it turns up as the word "None" as many times is there are comma seperated values in that field. (If there are 3 service areas it will come up as None, None, None, if 4 service areas: None, None, None, None etc.)
The rest of my data shows up correctly in the template. I'm kinda stumped and any ideas are welcome.
example output for the image I added above:
I have two values in the other_areas field (43006,43001) and it shows up like this
There really isn't much source code here to go off of. But, it is clearly telling you that there is nothing there. You could not be passing the object to the front-end from your views which would make those template tags show None. Since you don't show what your model actually is, what data you are rendering from your views, nobody here can necessarily help you. I would recommend you post code so we can further understand how to help you.

Retrieve extra fields with Graph API using PHP

I'm trying to create some social dashboard and therefore I want to retrieve my posts from my page. When I use this one to fetch my posts, it doesn't return me all the information I need (e.g 'picture', 'full_picture', 'attachments')
$user_posts = $facebook->api('/me/posts', 'GET');
print_r($user_posts);
But when I try next one, it still doesn't return me my required information:
$user_posts = $facebook->api('/me/posts?{created_time,id,message,full_picture,picture,attachments{url,subattachments},likes{name},comments{from,message,comment_count,user_likes,likes{name}}}', 'GET');
print_r($user_posts);
Anyone ideas??
I know that this has been asked a long time ago, but maybe useful for someone:
After - me/posts? - you need to make sure to put fields= and then a list of fields required.
So this would be:
$user_posts = $facebook->api('/me/posts?fields={created_time,id,message,full_picture,picture,attachments{url,subattachments},likes{name},comments{from,message,comment_count,user_likes,likes{name}}}', 'GET');
print_r($user_posts);

Can I change Django's auth_user.username field to be 100 chars long without breaking anything?

Before somebody marks this question as a duplicate of this question Can django's auth_user.username be varchar(75)? How could that be done? or other such questions on SO, please read this question. The question I linked to asks this question precisely but unfortunately the answers don't address the question that was asked.
Can I change the auth_user.username field to be 100 characters long by doing the following:
Run ALTER table in DB for the username field
Change the max_length here: username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and #/./+/-/_ characters"))
Would it break anything in Django if I were to do this?
That this will break when I update Django to a higher version is not a problem. I'm also not looking at writing other authentication methods.I just want to know if I would break anything if I were to do this.
You need to monkey-patch max-length in several places: model-field's description, form-field's description and max-length validators. Max-length validators are attached to form-fields as well as model-fields.
Here is a code snippet, which will patch everything:
https://gist.github.com/1143957 (tested with django 1.2 and 1.3)
Update:
Good news! Since django 1.5 you can override user model: Customizing the User model
There is no harm in doing that.
Just change the length in the model and in the database :)
Create a user profile model, add your very-long-username field there, and use it. of course this renders the genuine username field useless, but it is much better than hacking Django code, which will get you into trouble when you need to upgrade it.
For me, the code below works and is simple well.
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
....
# your custom fields
MyUser._meta.get_field('username').max_length = 255 # or 100
after you can run your migration
If you change the database manually as well as the model accordingly then there should be no problem.
You can also change back otherwise, and I would say make a backup just in case but I'm not sure its even necessary
for future needs this is the best and easiest way i found out:
https://github.com/GoodCloud/django-longer-username
Another solution is to change your authentication code. When a new user signs up, they enter an email, you save this in the email field so you have it, and if their email is >30 characters, you shorten it before putting it into the user field. Then, change your login authentication for new logins.