Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implemented the callback

  • Loading branch information...
commit 9f6482c39b52cdef6a24b084d803a1bd8c5e8354 1 parent 788d9dd
@brosner brosner authored
View
12 contacts_import/oauth_consumer.py
@@ -3,6 +3,7 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
+from django.core.urlresolvers import reverse
from django.utils import simplejson as json
import oauth2 as oauth
@@ -63,11 +64,13 @@ def unauthorized_token(self):
return self._unauthorized_token
def fetch_unauthorized_token(self):
+ base_url = "http://contacts-import.pinaxproject.com"
+ callback_url = reverse("oauth_callback", kwargs={"service": self.service})
request = oauth.Request.from_consumer_and_token(self.consumer,
http_url = self.request_token_url,
parameters = {
# @@@ fixme
- "oauth_callback": "http://contacts-import.pinaxproject.com/c/import_contacts/",
+ "oauth_callback": "%s%s" % (base_url, callback_url),
}
)
request.sign_request(self.signature_method, self.consumer, None)
@@ -89,6 +92,13 @@ def authorized_token(self, token):
except KeyError:
raise ServiceFail()
+ def check_token(self, unauth_token, given_token):
+ token = oauth.Token.from_string(unauth_token)
+ if token.key == given_token:
+ return self.authorized_token(token)
+ else:
+ return None
+
def authorization_url(self, token):
request = oauth.Request.from_consumer_and_token(
self.consumer,
View
1  contacts_import/urls.py
@@ -10,4 +10,5 @@
"redirect_to": "/contacts/import_contacts/",
}, name="bbauth_login"),
url(r"oauth/login/(?P<service>\w+)/", "contacts_import.views.oauth_login", name="oauth_login"),
+ url(r"oauth/callback/(?P<service>\w+)/", "contacts_import.views.oauth_callback", name="oauth_callback"),
)
View
17 contacts_import/views.py
@@ -68,6 +68,7 @@ def import_contacts(request, runner_class=AsyncRunner):
"form": form,
"bbauth_token": request.session.get("bbauth_token"),
"authsub_token": request.session.get("authsub_token"),
+ "linkedin_token": request.session.get("linkedin_token"),
"page": page,
"task_id": request.session.pop("import_contacts_task_id", None),
}, context_instance=RequestContext(request))
@@ -92,3 +93,19 @@ def oauth_login(request, service):
token = consumer.unauthorized_token()
request.session["%s_unauth_token" % service] = token.to_string()
return HttpResponseRedirect(consumer.authorization_url(token))
+
+
+def oauth_callback(request, service):
+ ctx = RequestContext(request)
+ consumer = oAuthConsumer(service)
+ unauth_token = request.session.get("%s_unauth_token" % service, None)
+ if unauth_token is None:
+ ctx.update({"error": "token_missing"})
+ else:
+ auth_token = consumer.check_token(unauth_token, request.GET.get("oauth_token", "no_token"))
+ if auth_token:
+ request.session["%s_token" % service] = (auth_token.key, auth_token.secret)
+ return HttpResponseRedirect(reverse("import_contacts"))
+ else:
+ ctx.update({"error": "token_mismatch"})
+ return render_to_response("oauth_error.html", ctx)
Please sign in to comment.
Something went wrong with that request. Please try again.