mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
Adding Twitter OAuth. Syncing friends for both twitter and facebook. Needs disconnect and resync.
This commit is contained in:
parent
52e49fec9f
commit
bfbba21711
4 changed files with 86 additions and 7 deletions
|
@ -1,5 +1,7 @@
|
|||
import datetime
|
||||
import zlib
|
||||
import mongoengine as mongo
|
||||
from django.conf import settings
|
||||
from vendor import facebook
|
||||
from vendor import tweepy
|
||||
|
||||
|
@ -53,7 +55,6 @@ class MSocialServices(mongo.Document):
|
|||
facebook_access_token = mongo.StringField()
|
||||
facebook_friend_ids = mongo.ListField(mongo.StringField())
|
||||
facebook_picture_url = mongo.StringField()
|
||||
facebook_username = mongo.StringField()
|
||||
facebook_refresh_date = mongo.DateTimeField()
|
||||
|
||||
meta = {
|
||||
|
@ -62,8 +63,36 @@ class MSocialServices(mongo.Document):
|
|||
'allow_inheritance': False,
|
||||
}
|
||||
|
||||
def sync_facebook_friends(self):
|
||||
def twitter_api(self):
|
||||
twitter_consumer_key = settings.TWITTER_CONSUMER_KEY
|
||||
twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET
|
||||
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
|
||||
auth.set_access_token(self.twitter_access_key, self.twitter_access_secret)
|
||||
api = tweepy.API(auth)
|
||||
return api
|
||||
|
||||
def facebook_api(self):
|
||||
graph = facebook.GraphAPI(self.facebook_access_token)
|
||||
return graph
|
||||
|
||||
def sync_twitter_friends(self):
|
||||
api = self.twitter_api()
|
||||
if not api:
|
||||
return
|
||||
|
||||
friend_ids = list(unicode(friend.id) for friend in tweepy.Cursor(api.friends).items())
|
||||
if not friend_ids:
|
||||
return
|
||||
|
||||
twitter_user = api.me()
|
||||
self.twitter_picture_url = twitter_user.profile_image_url
|
||||
self.twitter_username = twitter_user.screen_name
|
||||
self.twitter_friend_ids = friend_ids
|
||||
self.twitter_refreshed_date = datetime.datetime.utcnow()
|
||||
self.save()
|
||||
|
||||
def sync_facebook_friends(self):
|
||||
graph = self.facebook_api()
|
||||
if not graph:
|
||||
return
|
||||
|
||||
|
@ -71,6 +100,7 @@ class MSocialServices(mongo.Document):
|
|||
if not friends:
|
||||
return
|
||||
|
||||
facebook_friend_ids = [friend["id"] for friend in friends["data"]]
|
||||
facebook_friend_ids = [unicode(friend["id"]) for friend in friends["data"]]
|
||||
self.facebook_friend_ids = facebook_friend_ids
|
||||
self.save()
|
||||
self.facebook_refresh_date = datetime.datetime.utcnow()
|
||||
self.save()
|
||||
|
|
|
@ -95,8 +95,45 @@ def shared_stories_public(request, username):
|
|||
|
||||
@login_required
|
||||
def twitter_connect(request):
|
||||
tweepy
|
||||
pass
|
||||
twitter_consumer_key = settings.TWITTER_CONSUMER_KEY
|
||||
twitter_consumer_secret = settings.TWITTER_CONSUMER_SECRET
|
||||
|
||||
oauth_token = request.REQUEST.get('oauth_token')
|
||||
oauth_verifier = request.REQUEST.get('oauth_verifier')
|
||||
denied = request.REQUEST.get('denied')
|
||||
if denied:
|
||||
pass
|
||||
elif oauth_token and oauth_verifier:
|
||||
try:
|
||||
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
|
||||
auth.set_request_token(oauth_token, oauth_verifier)
|
||||
access_token = auth.get_access_token(oauth_verifier)
|
||||
api = tweepy.API(auth)
|
||||
twitter_user = api.me()
|
||||
except (tweepy.error.TweepError, IOError):
|
||||
return json.json_response(request, dict(error="Twitter has returned an error. Try connecting again."))
|
||||
|
||||
# Be sure that two people aren't using the same Twitter account.
|
||||
existing_user = MSocialServices.objects.filter(twitter_uid=unicode(twitter_user.id))
|
||||
if existing_user and existing_user[0].user_id != request.user.pk:
|
||||
user = User.objects.get(pk=existing_user[0].user_id)
|
||||
return json.json_response(request, dict(error=("Another user (%s, %s) has "
|
||||
"already connected with those Twitter credentials."
|
||||
% (user.username, user.email_address))))
|
||||
|
||||
social_services, _ = MSocialServices.objects.get_or_create(user_id=request.user.pk)
|
||||
social_services.twitter_uid = unicode(twitter_user.id)
|
||||
social_services.twitter_access_key = access_token.key
|
||||
social_services.twitter_access_secret = access_token.secret
|
||||
social_services.save()
|
||||
social_services.sync_twitter_friends()
|
||||
return json.json_response(request, dict(code=1))
|
||||
else:
|
||||
# Start the OAuth process
|
||||
auth = tweepy.OAuthHandler(twitter_consumer_key, twitter_consumer_secret)
|
||||
auth_url = auth.get_authorization_url()
|
||||
return HttpResponseRedirect(auth_url)
|
||||
|
||||
|
||||
@login_required
|
||||
def facebook_connect(request):
|
||||
|
@ -132,9 +169,10 @@ def facebook_connect(request):
|
|||
# Be sure that two people aren't using the same Facebook account.
|
||||
existing_user = MSocialServices.objects.filter(facebook_uid=uid)
|
||||
if existing_user and existing_user[0].user_id != request.user.pk:
|
||||
user = User.objects.get(pk=existing_user[0].user_id)
|
||||
return json.json_response(request, dict(error=("Another user (%s, %s) has "
|
||||
"already connected with those Facebook credentials."
|
||||
% (existing_user[0].username, existing_user[0].email_address))))
|
||||
% (user.username, user.email_address))))
|
||||
|
||||
social_services, _ = MSocialServices.objects.get_or_create(user_id=request.user.pk)
|
||||
social_services.facebook_uid = uid
|
||||
|
|
|
@ -69,3 +69,12 @@ if len(logging._handlerList) < 1:
|
|||
S3_ACCESS_KEY = 'XXX'
|
||||
S3_SECRET = 'SECRET'
|
||||
S3_BACKUP_BUCKET = 'newsblur_backups'
|
||||
|
||||
# ===============
|
||||
# = Social APIs =
|
||||
# ===============
|
||||
|
||||
FACEBOOK_APP_ID = '111111111111111'
|
||||
FACEBOOK_SECRET = '99999999999999999999999999999999'
|
||||
TWITTER_CONSUMER_KEY = 'ooooooooooooooooooooo'
|
||||
TWITTER_CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
||||
|
|
|
@ -439,6 +439,8 @@ REDIS = {
|
|||
|
||||
FACEBOOK_APP_ID = '111111111111111'
|
||||
FACEBOOK_SECRET = '99999999999999999999999999999999'
|
||||
TWITTER_CONSUMER_KEY = 'ooooooooooooooooooooo'
|
||||
TWITTER_CONSUMER_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
|
||||
|
||||
# ==================
|
||||
# = Configurations =
|
||||
|
|
Loading…
Add table
Reference in a new issue