def authfacebook(request): if request.user.is_authenticated(): return HttpResponseRedirect('/profile/') if request.GET.has_key('code'): code = request.GET['code'] #HACER CONSULTA A FACEBOOK POR EL TOKEN try: response = urllib2.urlopen("https://graph.facebook.com/oauth/" "access_token?client_id=" + settings.FACEBOOK_APP_ID + "&" "redirect_uri=" + request.build_absolute_uri() + "&client_secret=" + settings.FACEBOOK_APP_SECRET + "&code=" + code) except urllib2.HTTPError: return HttpResponseRedirect('/') access_token = urlparse.parse_qs(response.read())['access_token'][0] response.close() #utilizo el access_token para obtener los datos del usuario graph = facebook.GraphAPI(access_token) data = graph.get_object("me") facebook_id = data['id'] email = data['email'] name = data['name'] try: #ya esta registrado (login) MyUser.objects.get(email=email, facebook_id=facebook_id) except MyUser.DoesNotExist: try: user = MyUser.objects.get(email=email) user.facebook_id = facebook_id user.save() except MyUser.DoesNotExist: #recien se va a registrar (registro, creo el usuario) user = MyUser(email=email, facebook_id=facebook_id, name=name) user.save() #logueo con mi usuario facebook user = authenticate(email=email, facebook_id=facebook_id) if user is not None: login(request, user) return HttpResponseRedirect('/profile/') else: return HttpResponseRedirect('/login/') else: return HttpResponseRedirect("https://www.facebook.com/dialog/oauth?" "client_id=" + settings.FACEBOOK_APP_ID + "&" "redirect_uri=" + request.build_absolute_uri() + "&scope=publish_stream,email")
def authfacebook(request): if request.user.is_authenticated(): return HttpResponseRedirect('/profile/') if request.GET.has_key('code'): code = request.GET['code'] #HACER CONSULTA A FACEBOOK POR EL TOKEN try: response=urllib2.urlopen("https://graph.facebook.com/oauth/" "access_token?client_id=" + settings.FACEBOOK_APP_ID + "&" "redirect_uri=" + request.build_absolute_uri() + "&client_secret=" + settings.FACEBOOK_APP_SECRET + "&code="+code) except urllib2.HTTPError: return HttpResponseRedirect('/') access_token = urlparse.parse_qs(response.read())['access_token'][0] response.close() #utilizo el access_token para obtener los datos del usuario graph = facebook.GraphAPI(access_token) data = graph.get_object("me") facebook_id = data['id'] email = data['email'] name = data['name'] try: #ya esta registrado (login) MyUser.objects.get(email=email, facebook_id=facebook_id) except MyUser.DoesNotExist: try: user = MyUser.objects.get(email=email) user.facebook_id = facebook_id user.save() except MyUser.DoesNotExist: #recien se va a registrar (registro, creo el usuario) user = MyUser(email=email, facebook_id=facebook_id, name=name) user.save() #logueo con mi usuario facebook user = authenticate(email=email, facebook_id=facebook_id) if user is not None: login(request, user) return HttpResponseRedirect('/profile/') else: return HttpResponseRedirect('/login/') else: return HttpResponseRedirect("https://www.facebook.com/dialog/oauth?" "client_id="+settings.FACEBOOK_APP_ID+"&" "redirect_uri="+ request.build_absolute_uri() +"&scope=publish_stream,email")
def authlinkedin(request): if request.user.is_authenticated(): return HttpResponseRedirect('/profile/') if request.GET.has_key('code'): code = request.GET['code'] #print code #HACER CONSULTA A LINKEDIN POR EL TOKEN try: response=urllib2.urlopen("https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code="+code+ "&redirect_uri="+request.build_absolute_uri()+"&client_id="+settings.LINKEDIN_APP_ID+"&client_secret="+settings.LINKEDIN_APP_SECRET) except urllib2.HTTPError: return HttpResponseRedirect('/') #print response.read() #print json.loads(response.read()) access_token = json.loads(response.read())['access_token'] print access_token response.close() #utilizo el access_token para obtener los datos del usuario url='https://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,emailAddress)?' \ 'oauth2_access_token='+access_token try: response=urllib2.urlopen(url) my_linkedin_xml = response.read() response.close() except urllib2.HTTPError: return HttpResponseRedirect('/') xmldoc = minidom.parseString(my_linkedin_xml) item=xmldoc.getElementsByTagName('first-name') first_name = item[0].firstChild.nodeValue item=xmldoc.getElementsByTagName('last-name') last_name = item[0].firstChild.nodeValue item=xmldoc.getElementsByTagName('email-address') email = item[0].firstChild.nodeValue item=xmldoc.getElementsByTagName('id') linkedin_id = item[0].firstChild.nodeValue name = first_name + ' ' + last_name try: #ya esta registrado (login) MyUser.objects.get(email=email, linkedin_id=linkedin_id) except MyUser.DoesNotExist: try: user = MyUser.objects.get(email=email) user.linkedin_id = linkedin_id user.save() except MyUser.DoesNotExist: #recien se va a registrar (registro, creo el usuario) user = MyUser(email=email, linkedin_id=linkedin_id, name=name) user.save() #logueo con mi usuario linkedin user = authenticate(email=email, linkedin_id=linkedin_id) if user is not None: login(request, user) return HttpResponseRedirect('/profile/') else: return HttpResponseRedirect('/login/') else: return HttpResponseRedirect("https://www.linkedin.com/uas/oauth2/authorization?" "response_type=code&client_id="+settings.LINKEDIN_APP_ID+ "=r_fullprofile%20r_emailaddress" "&state=DCEEFWF45453sdffef424&redirect_uri="+request.build_absolute_uri())
def authlinkedin(request): if request.user.is_authenticated(): return HttpResponseRedirect('/profile/') if request.GET.has_key('code'): code = request.GET['code'] #print code #HACER CONSULTA A LINKEDIN POR EL TOKEN try: response = urllib2.urlopen( "https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=" + code + "&redirect_uri=" + request.build_absolute_uri() + "&client_id=" + settings.LINKEDIN_APP_ID + "&client_secret=" + settings.LINKEDIN_APP_SECRET) except urllib2.HTTPError: return HttpResponseRedirect('/') #print response.read() #print json.loads(response.read()) access_token = json.loads(response.read())['access_token'] print access_token response.close() #utilizo el access_token para obtener los datos del usuario url='https://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,emailAddress)?' \ 'oauth2_access_token='+access_token try: response = urllib2.urlopen(url) my_linkedin_xml = response.read() response.close() except urllib2.HTTPError: return HttpResponseRedirect('/') xmldoc = minidom.parseString(my_linkedin_xml) item = xmldoc.getElementsByTagName('first-name') first_name = item[0].firstChild.nodeValue item = xmldoc.getElementsByTagName('last-name') last_name = item[0].firstChild.nodeValue item = xmldoc.getElementsByTagName('email-address') email = item[0].firstChild.nodeValue item = xmldoc.getElementsByTagName('id') linkedin_id = item[0].firstChild.nodeValue name = first_name + ' ' + last_name try: #ya esta registrado (login) MyUser.objects.get(email=email, linkedin_id=linkedin_id) except MyUser.DoesNotExist: try: user = MyUser.objects.get(email=email) user.linkedin_id = linkedin_id user.save() except MyUser.DoesNotExist: #recien se va a registrar (registro, creo el usuario) user = MyUser(email=email, linkedin_id=linkedin_id, name=name) user.save() #logueo con mi usuario linkedin user = authenticate(email=email, linkedin_id=linkedin_id) if user is not None: login(request, user) return HttpResponseRedirect('/profile/') else: return HttpResponseRedirect('/login/') else: return HttpResponseRedirect( "https://www.linkedin.com/uas/oauth2/authorization?" "response_type=code&client_id=" + settings.LINKEDIN_APP_ID + "=r_fullprofile%20r_emailaddress" "&state=DCEEFWF45453sdffef424&redirect_uri=" + request.build_absolute_uri())