def auth(request): """We dont need a full oauth setup just a username. """ # http://flickr.com/services/auth/?api_key=[api_key]&perms=[perms]&api_sig=[api_sig] service = get_model_instance(request.user, PACKAGE_NAME) if service and request.method == "POST": username = request.POST["username"] if username: # Talk to flickr to get a flickr ID 1234567@N0 style url = ( "http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=%s&username=%s&format=json&nojsoncallback=1" % (service.app.auth_settings["api_key"], username) ) http_requester = httplib2.Http() resp, content = http_requester.request(url, "GET") if resp["status"] == "200": json = simplejson.loads(content) # Parse the request and check we have got back flickr id if json["stat"] == "ok": userid = json["user"]["id"] if not service.auth: auth_handler = Auth() else: auth_handler = service.auth auth_handler.username = username auth_handler.secret = userid auth_handler.save() if not service.auth: service.auth = auth_handler service.setup = True service.public = True service.save() else: messages.error( request, "Unable to validate your username with Flickr, please check your username and retry." ) else: messages.error(request, "Please enter a Flickr username") return redirect(settings_redirect(request))
def auth(request): """We dont need a full oauth setup just a username. """ service = get_model_instance(request.user, PACKAGE_NAME) if service and request.method == 'POST': username = request.POST['username'] if username: # Fetch the page and try to find the reader id url = "http://www.google.com/reader/shared/%s" % (username,) links = SoupStrainer('link') h = httplib2.Http() resp, content = h.request(url, "GET") parsed_links = [tag for tag in soup(str(content), parseOnlyThese=links)] if parsed_links: try: user_id = parsed_links[2].attrs[2][1].split('%2F')[1] if not service.auth: auth_handler = Auth() else: auth_handler = service.auth auth_handler.username = username auth_handler.secret = user_id auth_handler.save() if not service.auth: service.auth = auth_handler service.setup = True service.public = True service.save() except: messages.error( request, 'Unable to find Google Reader account with username "%s"' % (username,) ) else: messages.error( request, 'Unable to find Google Reader account with username "%s"' % (username,) ) else: messages.error(request, "Please enter a Google Reader username") return redirect(settings_redirect(request))