Εισαγωγή στο HTTP Basic Authentication με το Twitter API
Στέλιος Πετράκης, στις 12 November 2008 · Ανάπτυξη ·
Η ύπαρξη μιας πληθώρας από APIs στις μέρες μας (περί τα 1013 κατά το ProgrammableWeb), δίνει την δυνατότητα σε όλους να πειραματιστούν με τα δεδομένα αρκετών web services, είτε έχουν βασικές γνώσεις προγραμματισμού, είτε είναι πιο προχωρημένοι.
Οι λειτουργίες που προσφέρουν τα APIs στους developers παρόλα αυτά, δεν περιορίζονται μόνο στην αποστολή πληροφορίας από τις υπηρεσίες. Αρκετά από αυτά προχωράνε ένα ακόμη βήμα, προσφέροντας δυνατότητα πιστοποίησης των χρηστών της υπηρεσίας με πολλαπλούς τρόπους. Κάποιες υπηρεσίες χρησιμοποιούν συστήματα πιστοποίησης βασισμένα σε tokens (όπως το Flickr), ενώ άλλες έχουν ενστερνιστεί πρότυπα όπως το oAuth. Τα περισσότερα authentication-aware APIs παρόλα αυτά, υποστηρίζουν ένα αρκετά απλό σύστημα πιστοποίησης που ακούει στο όνομα HTTP Basic Authentication.
Μια αρκετά γνωστή υπηρεσία που παρέχει δυνατότητα πιστοποίησης των χρηστών μέσω του HTTP Basic Authentication, είναι το Twitter. Αλλά ας δούμε πρώτα τι ακριβώς είναι το HTTP Basic Authentication.
Τι είναι το HTTP Basic Authentication;
Το HTTP Basic Authentication πρόκειται για ένα σύστημα αποστολής των στοιχείων του χρήστη κατά την αίτηση μιας σελίδας/resouce από το web. Για να γίνει κάτι τέτοιο δυνατό, από πλευράς PHP, ο προγραμματιστής θα πρέπει να δημιουργήσει ένα socket (ένα κανάλι διπλής κατεύθυνσης μεταξύ δύο συνδεδεμένων υπολογιστών) και να γράψει όλη την αίτηση μέσα στο socket αυτό. Το username και το password του χρήστη που πρόκειται να πιστοποιηθεί, αποστέλονται με κωδικοποίηση base-64 με την μορφή base64(username:password).
Βασικός μας στόχος σε αυτό το tutorial, είναι να περιγραφεί ο τρόπος που μπορεί κάποιος να πετύχει την παραπάνω μορφή πιστοποίησης, χρησιμοποιώντας το Twitter API, ώστε τελικά να στείλει ένα tweet από το mashup του.
Βήμα 1: Δημιουργία της σελίδας με την φόρμα
Παρακάτω θα δημιουργήσουμε μια απλή φόρμα με 3 πεδία: username, password και το μήνυμα προς αποστολή.
Δημιουργήστε ένα νέο .php αρχείο (π.χ. twitter.php) και αντιγράψτε τον παρακάτω κώδικα:
Με την προϋπόθεση πως τρέχετε κάποιον web server και έχετε τοποθετήσει το αρχείο αυτό στον σωστό φάκελο (π.χ. htdocs ή ανάλογα τις ρυθμίσεις σας), πληκτρολογώντας το url της σελίδας στον browser, θα πρέπει να δείτε κάτι σαν το παρακάτω:

Βήμα 2: Επιλέξτε τον τρόπο για το HTTP Basic Authentication που σας ταιριάζει
Όπως αναφέραμε, το Twitter χρησιμοποιεί το HTTP Basic Authentication, αυτό όμως δεν σημαίνει απαραίτητα πως πρέπει να γράψετε όλη την αίτηση χειροκίνητα. Στην πραγματικότητα υπάρχουν 2 τρόποι για να αποστείλει κάποιος ένα tweet από το twitter mashup του.
Curl
Οι περισσότεροι web servers έχουν εγκατεστημένη την βιβλιοθήκη libcurl, μια δωρεάν URL transfer library, που κάνει την όλη διαδικασία ευκολότερη.
Έτσι λοιπόν, ο απλός τρόπος είναι η κλήση των κατάλληλων PHP functions, με την curl να χειρίζεται όλα τα υπόλοιπα (credit στον Gareth Rushgrove για την αρχική έμπνευση).
Βουτιά στα δύσκολα: Δημιουργία του socket
Όπως είπαμε και πριν, η curl δεν είναι εγκατεστημένη σε όλους τους web servers. Αν και πάντα υπάρχει η δυνατότητα εγκατάστασης της βιβλιοθήκης από τους administrators, παρόλα αυτά ας εξετάσουμε τι θα έπρεπε να κάνουμε αν προγραμματίζαμε τα πάντα μόνοι μας. Μην φοβάστε, δεν είναι τόσο δύσκολα τα πράγματα!
Βήμα 3: Ενσωμάτωση του κώδικα στην σελίδα
Όπως μπορείτε να δείτε παραπάνω, οι δύο τρόποι μοιράζονται αρκετά κοινά χαρακτηριστικά και αυτό γιατί υλοποιούν και οι δύο το ίδιο σύστημα πιστοποίησης.
Αφού επιλέξετε τον τρόπο που σας ταιριάζει αντιγράψτε τον κάτω από το σχόλιο της PHP "HTTP Basic Authentication PHP Code to be added here" στην σελίδα που δημιουργήσατε πιο πριν και είστε έτοιμοι.
Δοκιμάστε να στείλετε ένα tweet!
Στέλιος Πετράκης
Ο Στέλιος Πετράκης (stelabouras) δηλώνει φανατικός gamer και web addict. Εργάζεται ως web developer στην Phaistos Networks, ολοκληρώνει το μεταπτυχιακό του, γράφει posts στο Wiggler.gr, αναπτύσσει και συντηρεί το Slideflickr.com και στον ελεύθερό του χρόνο πειραματίζεται με οτιδήποτε έχει σχέση με web, programming, design και games.
{5 σχόλια ως τώρα } + Νέο σχόλιο
DanielXU
http://screenologist.com/pdf/gold-lovd1/knthivezte.html elvis costume jewelry http://cngroupsl.com/lyrics-t3f/erlddelysm.html gold mining new zealand http://screensmith.com/archives/gurgaon-0c/ededentl.html australian surfer jewelry action http://cocopeatsrilanka.com/auto-liced/ddenllyt.html neo 2 platinum http://screensmith.com/archives/notes-ced1/rigin.html black hills gold jewely wholesalers http://screenologist.com/pdf/e-gold-e32/dmpryieme.html pearl jewelry armoire http://screenologist.com/pdf/gold-lovd1/flesot.html 1923 silver certificate price http://screenlinegroup.com/a-n-jewecd/bondith.html lariat clasp necklace http://screensmith.com/archives/cow-paped2/tonytoun.html crystal ringed toasting flutes http://screensmith.com/archives/cow-paped2/crinedisugn.html children charms gold cut out sms messging ring tones silver flatware affiliates antique setting engagement rings fiber art jewelry exclusively yours jewelry worcester county leatherman silver micra multi-tool sportcraft gold croquet set alexis silver gag platinum medicine gold and bullion sterling silver family heart pendant long john silver’s dining custom jaguar license plate costume multi strand pearl necklace burnt hand on ring jewelry lapel pin fork clutch plate kenmore 158.17033 pedro castillo jewelry eastwind silver chatham ma platinum earring jackets alaska jade jewelry jewelry supplies bolo ties silver mark shield kontakt player gold crack the pixies silver genuine diamond dust elgin finger ring watch dropship gold jewelry manufacturer wholesale easy beaded jewelry 15 projects antigue silver purse karen franco silver spring minnesota personal license plates gold placer areas in wyoming diamond core drilling company gif of a man’s ring southwest jewelry turquoise jewelry silver state mines gif of a man’s ring black hills gold ladies bracelot diamond in the back luda jeep wrangler bumper plate going platinum vintage jewelry hoffman sterling henry holland antique silver plastic plates wholesaler cartier refillable gold purse spray king silver flair serial numbers white gold products susan b anthony gold coins olypic weight plates cubes jewelry magnetic plate gold original using scott’s liquid gold buying jewelry in turkey spur nails jewelry sterling silver figaro 13mm necklace windshield wiper servo plate limiters antique plates in green pastel diamond dust shoes andy warhol gold coast activities what are contenential plates structural and plate blaster used silver chain styles identification diamond lodge manchester avon collectible jewelry cut diamond baguette gold band licscence plate frame azalea’s silver webb coolsat 5000 platinum user manual hotchkiss schreuder medallion coin silver blood diamonds movie sound clips antique poison ring blackhills gold rapid city close out diamond hoop earrings diamonds heart shaped rings wood glass necklaces diamond mine in arkasas engagement diamond price nightfall of diamonds gold digger t shirt diamond picks wilmington nc jewelry class triangle diamonds unreal gold patch native jewelry catalog silver platinum portland maine pokemon diamond snow temple amber evans ring retaining ring dimension luminous rings simulated gem stone ring taylord costume jewelry hearts diamond jewlers plate cutter bentley silver spur auto gemstone bracelets in sterling silver license plate placards queens greatest hits platinum <a > marble game tilt silver</a> <a > diamond selecting</a> <a > new gold dollars values</a> <a > gold graphics manufacturing co</a> <a > sin after sin diamond</a> <a > fimo clay jewelry</a> <a > styrofoam ring</a> <a > hidden security license plate cameras</a> <a > platinum services talking rock ga</a> <a > diamond jewelry blue nile</a>
JLBill
http://4j.xyifqs.dyndns.dk/eanianst.html texas high school sports http://u0.ooguhc.xxl.st/lyouthecalin.html goffstown high school http://jg.vpejlt.dyndns.dk/berithet.html phoenix college downtown phoenix arizona http://q0.pesojb.fr.cr/rwatedethe.html blackjack dealer school sahara vegas http://0w.ivuoq.dyndns.dk/qukier.html school girl masterbation videos http://hi.cuipc.ze.cx/rymnul.html george washington medical school http://g4.mrpfpv.dyndns.dk/deninded.html powell middle school spring hill fl http://hq.hunuh.dyndns.dk/byerofothol.html x express school loan http://3m.uqpia.dyndns.dk/zezleye.html kona pacific charter school http://hq.hunuh.dyndns.dk/trlougeee.html companionship in college indiana area high school reunion work reales in school lake station schools jedi knights jedi academy ideas for involving parents in school pre med tulsa community college progressive era women college education old school track jackets santo high school burkburnett high school 1966 catholic theology schools quaker schools in 19th century kuwai high school newport school ar laytonville high school central montcalm high school classifieds lyrics the academy ohio school district code moline christian school small private colleges logos academy york pennsylvania sporty s flight academy bayside high school in florida normal community high school ouran high school host club 15 accomack county public schools virginia coches bridge academy hanford high school california private schools in ontario naval academy graduate list aesop absence reporting for schools school health records california high school exit exam length schools in lavina montana kings college school west barnes lane school and texas letourneau college texas fargo public school district colleges for crime scene investigations spanking kid in school school uniform policy skirts st lawrence central school claiborne co tn schools web sites pro college chin strap hot school girl pictures longview high school clairmont college ca national college application thomson high school class of 1997 n c schools for troubled girls georgia technical schools construction classes joel e ferris high school hermitage school district scholastic’s magic school bus websites unblock myspace school boredom marion county school of radiology schools in hobart in santiam christian school adair village or claymont schools ohio george washington medical school levy county schools florida high school lesbian sex pics high school musical i be looking college major elementary education options charter school noblesville indiana create a school report card haddon township high school sports junior college region x philadelphia bible schools online accounting college top law schools in country college baseball regional predictions home school curriculum for sale college graduate program new hire jacksonville florida high school hot college girl video wisconsin school boards association are jackolanterns offensive in school emmanuel college brisbane new york college fund lutheran world relief school bags dougherty high school class of 1990 nyc pass academy school forensic labs work reales in school taylor schools mi muskegon community college michigan grundy high school reunion school book report worksheet list of college in phoenix arizona international sunday school commentary laura gabrick allen high school high school cheerleaders stretching thomas nelson community college weather christian schools in santa barbara ca volunteer hours needed for medical school cambelsport high school hamilton county fl school board fittness trainer school <a > bus blows its top magic school</a> <a > school injuries student court cases nsw</a> <a > rural winconsin college advisors email 2008</a> <a > online school forums</a> <a > genre science fiction middle school</a> <a > american academy of neurology expert witness</a> <a > perryton high school donna mann</a> <a > massage therapy school in monterey california</a> <a > kansas city christian school</a> <a > a to z school theme catalog</a>
observ8
Βουτιά στα δύσκολα: ... γραμμή 22 δεν παίζει
κάνε και ένα post για Oauth στο twitter θα είναι χρήσιμο
stelabouras
@observ8
Κάτι πήγε στραβά με τον τρόπο παρουσίασης του κώδικα, νόμιζε το http://www.twitter.com ήταν link και τα μπέρδεψε. Η γραμμή είναι η εξής:
$socket_message .= “Host: http://www.twitter.com\r\n”;
Ελπίζω στο comment να φανεί σωστά.
erf
efrgthbngf http://www.linkedin.com/in/fdggfhhgf youporn bi 3tge5thy http://www.linkedin.com/in/wefrgthbn what happened to youjizz