I need to check if my users liked or posted to pages. I've tried a few of the packages around (facebook_api, django-facebook) but cannot seem to login using my access_token, or get an fql request working.
This will be a cron job, not a page load. The user will not be logged in.
Does anyone have any ideas?
I'm the author of Django Facebook, this is how you could do it:
Implement a registration flow, have a look at the docs.
https://github.com/tschellenbach/Django-facebook
Now that your users can register the database will contain the access_token and facebook id for the users.
In your cronjob you can do things like:
from open_facebook.api import OpenFacebook
graph = OpenFacebook(access_token)
graph.get('%s/likes' % facebook_id)
graph.fql('SELECT ... FROM like WHERE post_id = A and user_id = B')
for a cronjob i would use
graph.batch_fql(dictionary_with_queries) for better performance
Related
Okay so, ordinary Django allows you to simply:
if request.user.is_authenticated:
I want to be able to do the same in Pyrebase. Have the views sort of already know which user has logged in based on the current session without having to sign the user in in all views.
I have tried:
def sign_in(request):
user = firebase.auth().sign_in_with_email_and_password('email', 'password')
user_token = firebase.auth().refresh(user['refreshToken']
request.session['session_id'] = user_token
I noticed this creates a session ID for me. But I don't know how to associate it with the current user and I know it has something to do with the refresh token.
If I don't check authentication, anyone can visit any page of my site without signing in.
Is it possible to use Flask-Social and Flask-Security if I only want to use Facebook Login, for example, for user registration and login, i.e. no local registration/login forms?
I looked through the Flask-Social example application and documentation but couldn't tell if this is possible. In the example application, users cannot login with Facebook unless they've previously registered. After registering with the example application, they can associate their Facebook account with their local account.
When I tried to call social.facebook.get_connection() I got an AttributeError 'AnonymousUser' object has no attribute 'id' because there's no current_user, which is defined by flask-security after registration/login.
This is doable without too much extra work using the #login_failed.connect_via decorator. With app as your instance of a Flask app, it would look like
#login_failed.connect_via(app):
def on_login_failed(sender, provider, oauth_response):
connection_values = get_connection_values_from_oauth_response(provider, oauth_response)
ds = current_app.security.datastore
user = ds.create_user( ... ) #fill in relevant stuff here
ds.commit()
connection_values['user_id'] = user.id
connect_handler(connection_values, provider)
login_user(user)
db.commit()
return render_template('success.html')
As for filling in the relevant stuff for creating the user, I just create a random string for the password, and haven't had issues leaving the email null. I also just included the exact same answer on the Flask-Social github page.
I'm trying to use gdata in order to get the contact from my users on my site.
so far I have made a pip install gdata.
Then I added on top of my views.py
CLIENT_ID = 'mythingy' # Provided in the APIs console
CLIENT_SECRET = 'mythingy' # Provided in the APIs console
SCOPE = 'https://www.google.com/m8/feeds'
USER_AGENT = 'dummy-sample'
I have a button on my page that doesn nothing but I would like that if you click it it should ask you for persmission then open a pop up with the gmail contact of the user.
I would like to know how to implement htat.
in my views so far I have:
#login_required
def activate_user(request, activation_key):
user = User.objects.get(username=request.user)
profile = user.get_profile()
auth_token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID,client_secret=CLIENT_SECRET,scope=SCOPE,user_agent=USER_AGENT)
APPLICATION_REDIRECT_URI = 'http://127.0.0.1:8000/oauth2callback'
authorize_url=auth_token.generate_authorize_url(redirect_uri=APPLICATION_REDIRECT_URI)
return render_to_response('registration/activation_complete.html',{'user': user,'profile':profile}, context_instance=RequestContext(request))
But this does nothing.
I haven't touch my urls.py or my settings.py
Thank you
I end up using the javascript API. If anyone needs the code let me know
I have started using https://github.com/omab/django-social-auth and been successfully able to login via twitter, google and facebook.
Needed
I need to query about the logged in user in order to do more things, which Model I shall be using for that?
I don't see any examples for that
Thank you
Update
#Omab, I did not understand how this would work, can you please help. When I login with twitter, the callback goes to following code
#login_required
def done(request):
"""Login complete view, displays user data"""
ctx = {
'version': version,
'last_login': request.session.get('social_auth_last_login_backend')
}
logging.warn('context - ' + str(ctx))
logging.warn('request - ' + str(request))
return render_to_response('home.html', ctx, RequestContext(request))
Can you tell me how can I access to user instance here?
Thank you
The app stores the social account details using the UserSocialAuth model, to retrieve any instance just do:
user.social_auth.filter(provider="...")
Where:
user is a User instance (request.user for current logged in user)
provider is a string with the provider name (facebook, twitter, etc)
The UserSocialAuth instance stores the needed tokens to call the needed API:
print user_social_auth.tokens
{...}
As suggested by K-man, you can try this (Identifying the backend provider of a logged in user):
request.user.social_auth.values_list('provider')
Other values that you can find in the values_list include: id, uid (for ex. facebook user id), user, extra_data (which contains the access_token)
Using django, I am authenticating the user through Google. I get the initial request tokens & redirect the user to google for auth. After which google redirects the user back to my website (using the redirect_url I provide).
At this point the request.user.id is None so is request.user.username why is this happening? I need the user ID to enter the access_tokens (that google sends me) into the DB.
Under what conditions can request.user object in Django be empty?
UPDATE1: When I get redirected back from Google with the url pattern as http://mywebsite.com/lserv?s=goog control comes back to my django views function, but django gives me the request.user object user as Anonymous user with no username or id. why?
UPDATE2:
all this is running on python manage.py runserver for now...
UPDATE3: Anybody faced anythn similar to this? basically, out of no reason the user in request clears out automatically & I get assigned as Anonymous user. All this happens between url requests from the user (from browser). Why?
Django's auth mechanism has nothing to do with Google's or any other auth service. If you want to integrate third party auth service with your Django site, you should do it youself.
If you're using oauth2 library, it's README has a section named "Logging into Django w/ Twitter" may help you.
If you are using oauth api from google. To get the user you have to do something like this
from google.appengine.api import oauth
# ...
try:
# Get the db.User that represents the user on whose behalf the
# consumer is making this request.
user = oauth.get_current_user()
except oauth.OAuthRequestError, e:
# The request was not a valid OAuth request.
# ...