SlideShare ist ein Scribd-Unternehmen logo
1 von 92
Downloaden Sie, um offline zu lesen
Integrating With
Third-Party APIs
     Randy Hoyt
       @randyhoyt
Overview


• APIs: Defined
• Three Examples:
    1. Get Treehouse Badges
    2. Get ShopLocket Products
    3. Post Facebook & Twitter Updates
• Best Practices
                                 @randyhoyt
API
Application Programming Interface
A point where two systems,
  subjects, organizations, etc.,
  meet and interact.


Interface, http://dictionary.com/browse/interface
[Keyboard]
[Remote Control]
[Remote Control]
[Lock and Key]
An interface for software
 components to communicate
 with each other.


Wikipedia: API, http://trhou.se/defineAPI
API Examples




• Android SDK: Camera API




                            @randyhoyt
import android.hardware.Camera;


Camera.getNumberOfCameras();
Camera.open(cameraId);
API Examples




• Android SDK: Camera API
• WordPress: Plugin API


                            @randyhoyt
<?php
/*
Plugin Name: Treehouse Badges
Description: Displays the badges ...
Version: 1.0
*/
?>


add_action('init','treehouse_badges_init');
...
API Examples




• Android SDK: Camera API
• WordPress: Plugin API
• HTML5: Canvas API

                            @randyhoyt
var canvas = document.getElementById("canvas");

var context = canvas.getContext("2d");

context.fillRect(10, 10, 40, 380, "#000000");

context.drawImage(img, x, y, 100, 77);
Web APIs
REST
Representational State Transfer
RESTful API
Example #1: Treehouse
[Treehouse screenshot]
JavaScript Object Notation:
 Text-based open standard
 designed for human-readable
 data interchange.


Wikipedia: JSON, http://trhou.se/defineJSON
{
     "firstName": "John",
     "lastName": "Smith",
     "address": {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": 10021
     },
     "phoneNumber": [
         {
             "type": "home",
             "number": "212 555-1234"
         },
         {
             "type": "fax",
             "number": "646 555-4567"
         }
     ]
 }

Wikipedia: JSON, http://trhou.se/defineJSON
[Treehouse JSON]
[HTML]
Asynchronous JavaScript and XML:
 Technique used by the browser to
 retrieve data from a server in the
 background without interfering with
 the existing page


Wikipedia: Ajax, http://trhou.se/defineAJAX
[AJAX Request Code]
[Badge Code]
[Badge Display]
<?php

$curl = curl_init();

curl_setopt_array($curl, array(

      CURLOPT_RETURNTRANSFER => 1,

      CURLOPT_URL => 'http://teamtreehou...

));

$resp = curl_exec($curl);

curl_close($curl);

?>
GET	
  Request	
  with	
  HTTPful




HTTPful, http://phphttpclient.com/
GET	
  Request	
  with	
  HTTParty




HTTParty, http://httparty.rubyforge.org/
GET	
  Request	
  with	
  WordPress	
  HTTP	
  API




WordPress, http://codex.wordpress.org/HTTP_API
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
Example #2: ShopLocket
[Screenshot of WP]
[Screenshot of WordPress]
[Screenshot of Product Picker]
[Screenshot of Product Picker]
[Screenshot of Shortcode]
[Screenshot of Website]
Authentication
OAuth
  An open protocol to allow secure
  authorization in a simple and standard
  method from web, mobile and desktop
  applications.




OAuth, http://oauth.net/
App ID
5f4a89f2d6655ac7a8859343d6d15257
9410e7b659b200e00aaf3721cf46269e


App Secret
fa963a1ef3702c16d934500f7621697e2
d6bfd05d3ef1751a32bdeb2a0599020
[ShopLocket]
[ShopLocket Login]
[ShopLocket]
/? code = 53fe... & state = auth...
$args:
          code = $_GET["code"]
        app id = [app id]
    app secret = [app secret]


           [GET Request]
$response = wp_remote_post(
               'https://www.shoplocket.com/'
                   . 'oauth/token',
               $args
            );
Token:
310826b20a535a422ccaa46d65c7065f
83e403b9218a38962ab4e43021b93585
[Token]
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
[Refresh Button]
Example #3: Facebook & Twitter
[Screenshot of Cultivatr]
[Screenshot of Edit Screen]
[Screenshot of Date Picker]
[Diagram]
API Questions




• Does it have to
  be real time?

• What if something
  goes wrong?

                      @randyhoyt
REST: Types of Requests



• GET          (Read)
• POST         (Create)
• PUT          (Update)
• DELETE       (Delete)

                            @randyhoyt
require	
  '../tmhOAuth.php';
  require	
  '../tmhUtilities.php';
  $tw	
  =	
  new	
  tmhOAuth(array(
  	
  	
  'consumer_key'	
  	
  	
  	
  =>	
  'APP_ID',
  	
  	
  'consumer_secret'	
  =>	
  'APP_SECRET',
  	
  	
  'user_token'	
  	
  	
  	
  	
  	
  =>	
  'A_USER_TOKEN',
  	
  	
  'user_secret'	
  	
  	
  	
  	
  =>	
  'A_USER_SECRET',
  ));

  $code	
  =	
  $tw-­‐>request(
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  'POST',
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $tw-­‐>url('1/statuses/update'),
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  array('status'	
  =>	
  'My	
  Tweet')
  	
  	
  	
  	
  	
  	
  	
  	
  	
  );

Twitter Libraries, https://dev.twitter.com/docs/twitter-libraries
Best Practices
Best Practices




• Read the Documentation
• Use Available Libraries
• Log Everything

                            @randyhoyt
Questions?
             @randyhoyt

Weitere ähnliche Inhalte

Was ist angesagt?

Django REST Framework における API 実装プラクティス | PyCon JP 2018
Django REST Framework における API 実装プラクティス | PyCon JP 2018Django REST Framework における API 実装プラクティス | PyCon JP 2018
Django REST Framework における API 実装プラクティス | PyCon JP 2018Masashi Shibata
 
5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniternicdev
 
Poisoning Google images
Poisoning Google imagesPoisoning Google images
Poisoning Google imageslukash4
 
Introduction to Elgg
Introduction to ElggIntroduction to Elgg
Introduction to Elggniteshnandy
 
Enter the app era with ruby on rails (rubyday)
Enter the app era with ruby on rails (rubyday)Enter the app era with ruby on rails (rubyday)
Enter the app era with ruby on rails (rubyday)Matteo Collina
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Introduction To Elgg 1224395615749768 9
Introduction To Elgg 1224395615749768 9Introduction To Elgg 1224395615749768 9
Introduction To Elgg 1224395615749768 9Mobicules Technologies
 
Primefaces Confess 2012
Primefaces Confess 2012Primefaces Confess 2012
Primefaces Confess 2012cagataycivici
 
Widgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
Widgets: Making Your Site Great and Letting Others Help - WordCamp VictoriaWidgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
Widgets: Making Your Site Great and Letting Others Help - WordCamp VictoriaJeff Richards
 
Enter the app era with ruby on rails
Enter the app era with ruby on railsEnter the app era with ruby on rails
Enter the app era with ruby on railsMatteo Collina
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2RORLAB
 
Getting the Most Out of OpenSocial Gadgets
Getting the Most Out of OpenSocial GadgetsGetting the Most Out of OpenSocial Gadgets
Getting the Most Out of OpenSocial GadgetsAtlassian
 

Was ist angesagt? (20)

Django REST Framework における API 実装プラクティス | PyCon JP 2018
Django REST Framework における API 実装プラクティス | PyCon JP 2018Django REST Framework における API 実装プラクティス | PyCon JP 2018
Django REST Framework における API 実装プラクティス | PyCon JP 2018
 
5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter
 
Poisoning Google images
Poisoning Google imagesPoisoning Google images
Poisoning Google images
 
Introduction to Elgg
Introduction to ElggIntroduction to Elgg
Introduction to Elgg
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
Enter the app era with ruby on rails (rubyday)
Enter the app era with ruby on rails (rubyday)Enter the app era with ruby on rails (rubyday)
Enter the app era with ruby on rails (rubyday)
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
AWS Elastic Beanstalk
AWS Elastic BeanstalkAWS Elastic Beanstalk
AWS Elastic Beanstalk
 
Index chrome
Index chromeIndex chrome
Index chrome
 
Introduction To Elgg 1224395615749768 9
Introduction To Elgg 1224395615749768 9Introduction To Elgg 1224395615749768 9
Introduction To Elgg 1224395615749768 9
 
Codegnitorppt
CodegnitorpptCodegnitorppt
Codegnitorppt
 
Dr.Repi
Dr.Repi Dr.Repi
Dr.Repi
 
Primefaces Confess 2012
Primefaces Confess 2012Primefaces Confess 2012
Primefaces Confess 2012
 
Widgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
Widgets: Making Your Site Great and Letting Others Help - WordCamp VictoriaWidgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
Widgets: Making Your Site Great and Letting Others Help - WordCamp Victoria
 
Plugins unplugged
Plugins unpluggedPlugins unplugged
Plugins unplugged
 
Enter the app era with ruby on rails
Enter the app era with ruby on railsEnter the app era with ruby on rails
Enter the app era with ruby on rails
 
ApacheCon 2005
ApacheCon 2005ApacheCon 2005
ApacheCon 2005
 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
 
Getting the Most Out of OpenSocial Gadgets
Getting the Most Out of OpenSocial GadgetsGetting the Most Out of OpenSocial Gadgets
Getting the Most Out of OpenSocial Gadgets
 

Ähnlich wie Api

Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)danwrong
 
Open Source Ajax Solution @OSDC.tw 2009
Open Source Ajax  Solution @OSDC.tw 2009Open Source Ajax  Solution @OSDC.tw 2009
Open Source Ajax Solution @OSDC.tw 2009Robbie Cheng
 
API Technical Writing
API Technical WritingAPI Technical Writing
API Technical WritingSarah Maddox
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
Motion Django Meetup
Motion Django MeetupMotion Django Meetup
Motion Django MeetupMike Malone
 
droidQuery: The Android port of jQuery
droidQuery: The Android port of jQuerydroidQuery: The Android port of jQuery
droidQuery: The Android port of jQueryPhDBrown
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineAndy McKay
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsNeil Crookes
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and PythonPiXeL16
 
[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho
[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho
[CB16] Esoteric Web Application Vulnerabilities by Andrés RianchoCODE BLUE
 
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example Anna Klepacka
 
HTML5 on Mobile
HTML5 on MobileHTML5 on Mobile
HTML5 on MobileAdam Lu
 
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...Raj Lal
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)Igor Bronovskyy
 

Ähnlich wie Api (20)

Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)Building @Anywhere (for TXJS)
Building @Anywhere (for TXJS)
 
Open Source Ajax Solution @OSDC.tw 2009
Open Source Ajax  Solution @OSDC.tw 2009Open Source Ajax  Solution @OSDC.tw 2009
Open Source Ajax Solution @OSDC.tw 2009
 
PHPUG Presentation
PHPUG PresentationPHPUG Presentation
PHPUG Presentation
 
API Technical Writing
API Technical WritingAPI Technical Writing
API Technical Writing
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Motion Django Meetup
Motion Django MeetupMotion Django Meetup
Motion Django Meetup
 
YAP / Open Mail Overview
YAP / Open Mail OverviewYAP / Open Mail Overview
YAP / Open Mail Overview
 
droidQuery: The Android port of jQuery
droidQuery: The Android port of jQuerydroidQuery: The Android port of jQuery
droidQuery: The Android port of jQuery
 
Jquery fundamentals
Jquery fundamentalsJquery fundamentals
Jquery fundamentals
 
Developing apps using Perl
Developing apps using PerlDeveloping apps using Perl
Developing apps using Perl
 
Cross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App EngineCross Domain Web
Mashups with JQuery and Google App Engine
Cross Domain Web
Mashups with JQuery and Google App Engine
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIs
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
 
Zendcon 09
Zendcon 09Zendcon 09
Zendcon 09
 
[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho
[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho
[CB16] Esoteric Web Application Vulnerabilities by Andrés Riancho
 
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
Workshop KrakYourNet2016 - Web applications hacking Ruby on Rails example
 
HTML5 on Mobile
HTML5 on MobileHTML5 on Mobile
HTML5 on Mobile
 
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
Hybrid Application Development for Maemo N900 Device using Qt Webkit - Discov...
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
 
huhu
huhuhuhu
huhu
 

Api