Download - Api

Transcript
Page 1: Api

@randyhoyt

Randy Hoyt

Integrating With Third-Party APIs

Page 2: Api
Page 3: Api

@randyhoyt

• APIs: Defined

• Three Examples:

1. Get Treehouse Badges

2. Get ShopLocket Products

3. Post Facebook & Twitter Updates

• Best Practices

Overview

Page 4: Api

API

Page 5: Api

Application Programming Interface

Page 6: Api

A point where two systems, subjects, organizations, etc., meet and interact.

Interface, http://dictionary.com/browse/interface

Page 7: Api

[Keyboard]

Page 8: Api

[Remote Control]

Page 9: Api

[Remote Control]

Page 10: Api

[Lock and Key]

Page 11: Api

An interface for software components to communicate with each other.

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

Page 12: Api

@randyhoyt

• Android SDK: Camera API

API Examples

Page 13: Api

import android.hardware.Camera;

Camera.getNumberOfCameras();

Camera.open(cameraId);

Page 14: Api

@randyhoyt

• Android SDK: Camera API

• WordPress: Plugin API

API Examples

Page 15: Api

<?php

/*

Plugin Name: Treehouse Badges

Description: Displays the badges ...

Version: 1.0

*/

?>

add_action('init','treehouse_badges_init');

...

Page 16: Api

@randyhoyt

• Android SDK: Camera API

• WordPress: Plugin API

• HTML5: Canvas API

API Examples

Page 17: Api

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

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

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

context.drawImage(img, x, y, 100, 77);

Page 18: Api

Web APIs

Page 19: Api

REST

Page 20: Api

Representational State Transfer

Page 21: Api

RESTful API

Page 22: Api

Example #1: Treehouse

Page 23: Api

[Treehouse screenshot]

Page 24: Api

JavaScript Object Notation:Text-based open standard designed for human-readable data interchange.

Wikipedia: JSON, http://trhou.se/defineJSON

Page 25: Api

{ "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

Page 26: Api

[Treehouse JSON]

Page 27: Api

[HTML]

Page 28: Api

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

Page 29: Api

[AJAX Request Code]

Page 30: Api

[Badge Code]

Page 31: Api

[Badge Display]

Page 32: Api
Page 33: Api
Page 34: Api
Page 35: Api
Page 36: Api
Page 37: Api
Page 38: Api
Page 39: Api
Page 40: Api

<?php

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_RETURNTRANSFER => 1,

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

));

$resp = curl_exec($curl);

curl_close($curl);

?>

Page 41: Api

GET  Request  with  HTTPful

HTTPful, http://phphttpclient.com/

Page 42: Api

GET  Request  with  HTTParty

HTTParty, http://httparty.rubyforge.org/

Page 43: Api

GET  Request  with  WordPress  HTTP  API

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

Page 44: Api
Page 45: Api
Page 46: Api
Page 47: Api
Page 48: Api
Page 49: Api

@randyhoyt

• Does it have to be real time?

• What if something goes wrong?

API Questions

Page 50: Api

Example #2: ShopLocket

Page 51: Api

[Screenshot of WP]

Page 52: Api

[Screenshot of WordPress]

Page 53: Api

[Screenshot of Product Picker]

Page 54: Api

[Screenshot of Product Picker]

Page 55: Api

[Screenshot of Shortcode]

Page 56: Api

[Screenshot of Website]

Page 57: Api

Authentication

Page 58: Api

OAuthAn open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.

OAuth, http://oauth.net/

Page 59: Api

App ID

5f4a89f2d6655ac7a8859343d6d152579410e7b659b200e00aaf3721cf46269e

App Secret

fa963a1ef3702c16d934500f7621697e2d6bfd05d3ef1751a32bdeb2a0599020

Page 60: Api

[ShopLocket]

Page 61: Api

[ShopLocket Login]

Page 62: Api

[ShopLocket]

Page 63: Api

/? code = 53fe... & state = auth...

Page 64: Api

[GET Request]

$args: code = $_GET["code"] app id = [app id] app secret = [app secret]

$response = wp_remote_post( 'https://www.shoplocket.com/' . 'oauth/token', $args );

Page 65: Api

Token:

310826b20a535a422ccaa46d65c7065f83e403b9218a38962ab4e43021b93585

Page 66: Api

[Token]

Page 67: Api

@randyhoyt

• Does it have to be real time?

• What if something goes wrong?

API Questions

Page 68: Api
Page 69: Api
Page 70: Api
Page 71: Api
Page 72: Api
Page 73: Api
Page 74: Api
Page 75: Api
Page 76: Api

[Refresh Button]

Page 77: Api
Page 78: Api

Example #3: Facebook & Twitter

Page 79: Api

[Screenshot of Cultivatr]

Page 80: Api

[Screenshot of Edit Screen]

Page 81: Api

[Screenshot of Date Picker]

Page 82: Api

[Diagram]

Page 83: Api
Page 84: Api
Page 85: Api
Page 86: Api
Page 87: Api

@randyhoyt

• Does it have to be real time?

• What if something goes wrong?

API Questions

Page 88: Api

@randyhoyt

• GET

• POST

• PUT

• DELETE

REST: Types of Requests

(Read)

(Create)

(Update)

(Delete)

Page 89: Api

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

Page 90: Api

Best Practices

Page 91: Api

@randyhoyt

• Read the Documentation

• Use Available Libraries

• Log Everything

Best Practices

Page 92: Api

Questions?@randyhoyt