How to find users by their facebook uid in python social auth? - python

I have parsed the friends data from the graph api and now I have the "friends" data which include the id of the friends.Now I am looking for a way to find users by the id, to add the User objects to each other as friends.
I know how to find one user's uid.
user = User.objects.get(......)
user.social_auth.get(provider='facebook').uid
But how do I find User object by the uid?

You can just do a Request on URL:
https://graph.facebook.com/<UID>
{
"id": "358204548",
"first_name": "someName",
"gender": "male",
"last_name": "someLastName",
"locale": "en_US",
"name": "John Doe",
"username": "john.doe"
}

What you want is the User object, not the user info, right?
I can't find it in any documentation, so I am not sure what's the best way to do it. But I found that this is how social_core get the user object during the login proccess: https://github.com/python-social-auth/social-core/blob/df861e886f6984f813c1f1c5c9a0acffbe719e0d/social_core/pipeline/social_auth.py#L19
So this works for me:
from social_django.models import UserSocialAuth
user = UserSocialAuth.get_social_auth('facebook', uid).user
Hope this helps

Related

get user profile photo from gmail api object

I build this google API client object,
serivce=build('gmail', 'v1', credentials=credentials)
i succed in founding the gmail address by doing this
serivce.users().getProfile(userId='me').execute()['emailAddress']
but I didn't found a way to get the Gmail user profile photo .
first- I tried to get it from getProfile but it only have history and other attributes.
then I tried some versions like getPhotos()/photos/getUrlPhoto but the service object doesn't have those attributes.
I would like to know how can I get from this object the user profile photo.
The Profile for a Gmail user contains the following information:
{
"emailAddress": string,
"messagesTotal": integer,
"threadsTotal": integer,
"historyId": string
}
Unfortunately, the profile photo is not one of them.
To retrieve a profile picture, you need to use the People API
Use the method people.get
Specify a resourceName (e.g. people/me) and set personFields to photos
This will return you the url(s) of the user's profile picture(s).
The response will look as follows:
{
"resourceName": "AAA",
"etag": "BBB",
"photos": [
{
"metadata": {
"primary": true,
"source": {
"type": "DOMAIN_PROFILE",
"id": "AAA"
}
},
"url": "https://lh3.googleusercontent.com/a-/CCC=s100"
}
]
}

Get education history by id Facebook

I was trying to extract Facebook user profile by GraphAPI. From the query:"https://graph.facebook.com/v2.4/search?q=vietnam&limit=1000&offset=0&type=user&format=json&access_token=....". I can get the list of id Facebook. But I want more information about user profile such as education, email. I used query:"https://graph.facebook.com/v2.4/{id_user}?fields= education,birthday&access_token=...."But I cant get them. Any suggestions for my problem? I will highly appreciated
In order to obtain more detailed infos such as 'education', 'birthday' and 'email', you need an access token with permissions that will grant you access to that info.
From the graph API explorer, with the token you get by default, using the following query:
me?fields=id,name,education,email,birthday
will not return education, email or birthday
{
"id": "...",
"name": "..."
}
Using the Graph API Explorer tool, you can try the same query with more priviledges.
Click the "Get Token" button, then on the prompt you get, check the following permissions:
user_birthday
user_education_history
email (located inside "extended permissions")
then try the request again. You should get:
{
"id": "...",
"name": "...",
"email": "...#....com",
"birthday": "../../...."
"education": ...
}
NOTE: You may not get all fields, depending on wether the target user has made these fields available to you.
Also if the user you're querying is 'yourself' (/me) then the user must have granted these permissions to your app. (a user may decline individual permissions. Your app must take that in consideration and handle cases where permissions were refused.)
Once you've confirmed this works, all you have to do is to add these permissions to your login or authorization flow inside your app.

Django REST Framework - Caching Embedded objects in viewset response

I've been caching list and retrieve methods for ViewSets in Django REST Framework using drf-extensions https://chibisov.github.io/drf-extensions/docs/#caching.
I wrote a modification to cache the key indefinitely for those list and retrieve methods which invalidates the keys for these methods when an object is modified.
The issue I am trying to solve is cache invalidation when an embedded object is modified. I am not familiar with the typical patterns for cache invalidation for embedded objects.
Ex:
{
"id": 1,
"position": "manager",
"company": "Google",
"user": {
"id": 1,
"email": "johnsmith#example.com",
"first_name": "John",
"last_name": "Doe",
"last_login": "2015-01-05T20:46:15.400Z",
"joined_at": "2014-12-10T19:54:45.588Z",
}
}
A user modifies the user object, ideally the cache key set for the list and retrieve methods for the user ViewSet along with the embedded object ViewSet should both be invalidated.
Any help is appreciated. Thanks.

Google Drive Permissions API – security settings

We are working with Google Drive and Permissions API to get e-mail addresses of users with sharing permissions. The API might not return the e-mail address if user forbids this in Google+ profile.
The documentation to Google Drive Permissions API says:
The email address of the user this permission refers to. This is an output-only field which is populated when the permission type is user and the given user's Google+ profile privacy settings allow exposing their email address.
After few hours of searching, we cannot find this "profile privacy settings" in question. Google Account Settings shows no such field.
Anybody knows where it is?
With "profile privacy settings" they mean that the Google + profile have shared the email from the contact information with the profile you are authenticated for the request.
I've made some test with a couple of documents, both shared with "Anyone with the link can view", but the first is from a user that shares with me the email in contact information.
Using the Try It I get the following result:
{
"kind": "drive#permission",
"etag": "\"SQFIsIrlQ4j3H07nwR6GyVXbP4s/4mfL2lpQkyuoRKmfVUHuvQuo5DI\"",
"id": "yyyyyyyyyyyy",
"selfLink": "https://content.googleapis.com/drive/v2/files/1ypzfcjfxIusRz0rLoDYh49DqPUj9DdZJI3lCGLtCkn8/permissions/yyyyyyyyyyyyy",
"name": "NAME Surname",
"emailAddress": "KKKKK.XXXXX#gmail.com",
"domain": "gmail.com",
"role": "owner",
"type": "user",
"photoLink": "https://lh4.googleusercontent.com/-DRgxkD0Vigg/AAAAAAAAAAI/AAAAAAAAZ0g/dszVFwA6bUo/s64/photo.jpg"
}
The second file is from a test account that doesn't share the email with me.
And I get the following:
{
"kind": "drive#permission",
"etag": "\"SQFIsIrlQ4j3H07nwR6GyVXbP4s/eUBKpMiVeQhLf3_Rf4Us5qisOqU\"",
"id": "ooooooooookkkkkkk",
"selfLink": "https://content.googleapis.com/drive/v2/files/1IpGtyYgJ_K6b0Xp2TWuYrcCTfPrIGJoUrStMPCyWGgM/permissions/kkkkkkkkkk",
"name": "lkajdlkj oiuaoiuoi",
"domain": "gmail.com",
"role": "owner",
"type": "user",
"photoLink": "https://lh3.googleusercontent.com/-SUCY0jPNqsA/AAAAAAAAAAI/AAAAAAAAMEk/8Y7SD9VNhHE/s64/photo.jpg"
}
As you can see the emailAddress is not displayed in the second result
To change this setting in your Google+ profile follow this steps (or the official help page):
Go to your account on https://plus.google.com
When the page is loaded in the top left corner, hover to the Home Menu
Click on Profile
Open the About tab of your profile (top center)
Scroll down to the Contact Information and click edit
Home and Work contacts can be shared to different circles or remove all the circles to make the contacts fully private

Tastypie: Don't send the whole uri of resource (just the id)

I am starting to add Tastypie to a very small Django application I'm developing, and I was wondering if there is a way to send just the numeric id of a resource pointed to by a relationship, instead of the uri where the resource resides.
For instance, using one of the examples provided in the documentation:
The exposed "Entry" resource looks like:
{
"body": "Welcome to my blog!",
"id": "1",
"pub_date": "2011-05-20T00:46:38",
"resource_uri": "/api/v1/entry/1/",
"slug": "first-post",
"title": "First Post",
"user": "/api/v1/user/1/"
}
It has a relationship towards "user" that shows as "user": "/api/v1/user/1/". Is there any way of just making it "user": 1 (integer, if possible) so it looks like the following?
{
"body": "Welcome to my blog!",
"id": "1",
"pub_date": "2011-05-20T00:46:38",
"resource_uri": "/api/v1/entry/1/",
"slug": "first-post",
"title": "First Post",
"user": 1
}
I like the idea or keeping the resource_uri attribute as whole, but when it comes to modeling Sql Relationships, I'd rather have just the id (or a list of numeric ids, if the relationship is "ToMany"). Would it be good idea adding a dehydrate_user method to the EntryResource class to do this? It seems to work, but maybe there's a more generic way of doing it (to avoid having to write a dehydrate method for every relationship)
Thank you in advance
You can try using hydrate dehydrate cycles
def dehydrate(self, bundle):
bundle.data['entry'] = bundle.obj.entry.id
return bundle
def hydrate(self, bundle):
bundle.data['entry'] = Entry.objects.get(id=bundle.data['entry'])
return bundle
BUT I strongly recommend to stick with URI usage since it is how you can adress directly a resource. Hydrate and dehydrate are used for more complexe or virtual resources.

Categories

Resources