What actually get_Object() takes as a parameter in Fabook SDK? - python

I want to send friend name as a user in get_object()in below code for getting his public posts. But I am getting error
raise GraphAPIError(result)
facebook.GraphAPIError: (#803) Cannot query users by their username (tayyab.rasheed.545)
user = 'tayyab.rasheed.545' #is giving error
#user = 'BillGates' #is working fine.
# user = 'me' #is working fine.
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')
Why is the error? I think I am doing something wrong. BillGates and me is working fine then why not tayyab.rasheed.545 Profile of friend is 'https://www.facebook.com/tayyab.rasheed.545'

Why is the error?
Because Facebook removed the username field from the API with v2.0, and as the error message says, you can not query user profiles by their username any more.
BillGates and me is working fine then why not `tayyab.rasheed.545
BillGates simply is a Facebook Page, and not a user profile.
(And me has nothing to do with the username in the first place.)

It is not possible to get public posts of any user. Even for you own public posts, you need to authorized yourself with the user_posts permission.
Edit: Please donĀ“t change your question, especially when there is an answer already. CBroes answer is correct. You are not supposed to get any data of users who did not authorized your App anyway.

Related

User email from GitHub in django social login

I followed this article to get social login in my django application.
It is working well, but for github users I can't get user email, even when I add
SOCIAL_AUTH_GITHUB_SCOPE = ['email']
What should I change in the article example to get user email?
Please change to:
SOCIAL_AUTH_GITHUB_SCOPE = ['user:email']

Name of the user not getting displayed in python code

I have made a signup page using python. Following is the link to the code :
Signup Page Code
As soon as a user signs up, he should be redirected to /welcome which says :
Welcome user
But I cannot display it by the above mentioned code.
Whereas when I write
self.redirect('/welcome?username = '+user)
I get redirected to a page which says
Welcome user
Why is it so?
When you write:
self.redirect('/welcome?username=' + user) #no whitespaces in the url
you are adding to the url a GET parameter(of the type name=value), in your case username=user.
So, when your welcomeHandler class handles the get request:
username=self.request.get('username')
it sets the username to the value of the GET parameter in the url with the name 'username' (username = user) and your welcome message is correctly displayed.
If you:
self.redirect('/welcome')
there are no GET parameters in the url and username is set to the default empty string and just Welcome is displayed.
Check here the Request documentation.

Customize error messages on django-registration

I have a question about django-registration (https://bitbucket.org/ubernostrum/django-registration), but I can't find their issue tracker or a mailing list for it, so I'll try my luck here.
My application enables login via OpenID and login/password.
Some users "forget their password" on FS on try to reset it (here), but then they get the message:
The user account associated with this e-mail address cannot reset the password.
With no further explanations.
(You can try and reset my password - just type my email (tonylampada at gmail dot com) there to see the error message.
I want to customize that message. A better message would be:
The user account associated with this e-mail address cannot reset the password.
This happens because the user account was created with an OpenID or OAuth provider (tipically Google, Facebook, MyOpenID, etc).
To see the login provider(s) associated with this account, take a look at the user profile.
What is the easiest way to tell django-registration that?
Thanks!
PS: This issue on Github: https://github.com/freedomsponsors/www.freedomsponsors.org/issues/191 (just in case you're feeling like making a pull request today :-))
django-registration uses views from django.contrib.auth.
In this case: reset_password() github
Since this is no class based view, you can't override/inherit from it, but you can pass in a PasswordResetForm from django.contrib.auth.forms
from django.contrib.auth.forms import PasswordResetForm
class CustomResetForm(PasswordResetForm):
def validate(self, value):
#pseudocode
if user.cant_reset_pw:
raise ValidationError("The user account associated with this e-mail address cannot reset the password. and so forth..")
super(CustomResetForm, self).validate(value)
You'll have to wire things together by overriding the url r'^password/change/$' to point to a custom function that calls django.contrib.auth.passwort_reset() with your CustomResetForm.

'AnonymousUser' object has no attribute 'backend'

Using django-socialregistration, got following error:
'AnonymousUser' object has no attribute 'backend'
How,
I click on facebook connect url.
That took me Facebook and ask me to login. So I did, asked permission, I granted.
After that it redirect me to my site. And ask to setup. I provide user and email address.
Once I submit, got error like above:
Trace point:
path/to_file/socialregistration/views.py in post
128. self.login(request, user)
Do anybody know, what's wrong?
Oh man i used to get this error all the time, basically you are calling
self.login(request, user)
without calling
authenticate(username=user, password=pwd)
first
when you call authenticate, django sets the backend attribute on the user, noting which backend to use, see here for more details
https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate
I had the same error for a newly registering user.
def attempt_login(self, email, password):
user = authenticate(username=email, password=password)
login(self.request, user)
return user
I checked into database and the User has been created after registration, but this error was still there.
I figured out - user's login ( email ) was longer than 30 characters, and the form field had no validation. The username would get truncated in the database, and therefore authenticate was called for non-existent login.
254 - character is the advised length of email field.
Solution: emailfield-max_length-r11092.patch
I just got this error and found this post.. My solution was in the case was in the registration process. When the user was registering, my api and serializer wasn't hashing the password.. So in the api_view i had to manually hash the password like this..
from django.contrib.auth.hashers import make_password
# In the register api..
#ensure_csrf_cookie
#api_view(['POST'])
def register_api(request):
# Anywhere before the serializer
request.DATA['password'] = make_password(request.DATA['password'])
# Then the serializer
serializer = RegisterSerializer(data=request.DATA)
# ... etc.. Note that if you want to login after register you will have
# to store the initial password is some buffer because.. authentication
# the none hashed version.. then
authenticate(username=request.DATA['username'], password=someBuffer)
Hope that helps someone..

GAE User registry without Google Accounts, want to restric to specific domain

I am planning on creating an application for the students of my school, and I want to restrict user registration to emails of the form person#myschool.edu. I would prefer to not manually create the user table and do the password hashing and such. Are there any libraries you can recommend for this?
Thanks for the help.
Sometimes, if you just send the user to a login screen you will end in a redirect loop if the user is already logged with a Google Account.
What i have found to be a good answer to this problem is to redirect the user to a log out page so he can later login with the domain you want.
I have used this for my code
user = users.get_current_user()
#Check if the user is in #mydomain.com
if user:
emailDomain = user.email().split("#")
if emailDomain[1] == "mydomain.com":
return True
else:
self.redirect(users.create_logout_url('/startPage'))
else:
self.redirect(users.create_login_url(self.request.uri))
This way, the application logs you out automatically and asks for your domain credentials
Since you said you don't know how the email are registered, that you don't want to manage a login/password database and you just need a regexp or somethings (I quote here!), I assume you could keep it very simple.
Something like.
user = users.get_current_user()
if user:
emailDomain = user.email().split("#")
if emailDomain == "yourschool.edu":
doSomething()
That way, all the trouble of registering to your app is given to the users (who will need to get a Google Account).

Categories