sfdc tournyc14 salesforceintegrationwithgoogledoubleclick__final_20141119

15
Salesforce Integration With Google Doubleclick Ami Assayag, Architect, CRM Science PhillyForce DUG Leader @AmiAssayag

Upload: ami-assayag

Post on 12-Jul-2015

312 views

Category:

Software


0 download

TRANSCRIPT

Salesforce Integration With

Google DoubleclickAmi Assayag,

Architect, CRM Science

PhillyForce DUG Leader

@AmiAssayag

Ami Assayag

Architect, PhillyForce DUG Leader

@AmiAssayag

Speaker

Digital Advertising Integration

• Doubleclick For Publishers (DFP)

• Manage Google AdWords Performance

• SOAP API (xml)

• Oauth 2.0 (json)

DFP

• Get account network code

• Enable API access

Google Developer Console

• Create a project (Google’s “Connected App”)

• Get Client Id and Client Secret

Project Setup

Create Project in the Console

Create

New

Client ID

Record

Client ID

and

Client

Secret

Get Authentication Codepublic string getAuthCodeUrl(string redirectUri) {

// use the URL used when authenticating a google user

pagereference pr = new pagereference(custSetting.AuthEndpoint__c + 'auth');

// add the necessary parameters

pr.getParameters().put('response_type', 'code');

pr.getParameters().put('client_id', custSetting.ClientId__c);

pr.getParameters().put('redirect_uri', redirectUri);

pr.getParameters().put('scope', custSetting.Scope__c);

// add required parameters needed to get an be able to use a refresh token

pr.getParameters().put('access_type', 'offline');

pr.getParameters().put('approval_prompt', 'force');

return pr.getUrl();

}

Exchange Code for Token

• Done so far:

– User logged in and gave consent

– Google directed user to the return URI

– Authorization code was included in the return URL

• Up next:

– Grab the authentication code from URL

– Make a callout to exchange authentication code for an access token

Get Access Tokenpublic void getAccessToken(string authCode) {

// prepare a string to send to google as request body

string body = 'code=' + authCode;

body += '&client_id= custSetting.ClientId__c;

body += '&client_secret= custSetting.ClientSecret__c;

body += '&redirect_uri= custSetting.redirectUri__c;

body += '&grant_type=authorization_code';

// HTTP callout to Google to exchange the auth code with an access token.

// use internal class to deserialize json response.

Authenticate(body);

}

Callout to DFP with Token

• Done so far:

– Saved access token

• Up next:

– Callout to get cool data!

Simplified Look at SOAP XML Body<envelope>

<Header>

<RequestHeader type="SoapRequestHeader">

<authentication type="OAuth">

<parameters>Bearer My_Access_Token</parameters>

</authentication>

<networkCode type="string">

My_Network_Code

</networkCode>

<applicationName type="string">

My_Google_Project_Name

</applicationName>

</RequestHeader>

</Header>

<Body>

My_SOAP_Body

</Body>

</envelope>

Properly Formatted SOAP XML Body<?xml version="1.0" encoding="UTF-8"?>

<env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<env:Header>

<RequestHeader actor="http://schemas.xmlsoap.org/soap/actor/next"

mustUnderstand="0" xsi:type="ns1:SoapRequestHeader"

xmlns:ns1="https://www.google.com/apis/ads/publisher/v201403"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ns1:authentication xsi:type="ns1:OAuth" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<ns1:parameters>Bearer My_Access_Token</ns1:parameters>

</ns1:authentication>

<ns1:networkCode xsi:type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

My_Network_Code

</ns1:networkCode>

<ns1:applicationName xsi:type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

My_Google_Project_Name

</ns1:applicationName>

</ns1:RequestHeader>

</env:Header>

<env:Body>

My_SOAP_Body

</env:Body>

</env:envelope>

Sample Calloutpublic void SimpleCalloutToGoogle(string endpoint, string method, string body){

// set up the request

HttpRequest req = new HttpRequest();

req.setEndpoint(endpoint);

req.setMethod(method);

req.setTimeout(10000);

// set the headers and body

req.setHeader('Content-Type', 'text/xml; charset=UTF-8');

req.setHeader('SOAPAction', '');

// make the callout

Http h = new Http();

HttpResponse res = h.send(req);

// inspect results with XmlStreamReader...

}

Ami Assayag

Architect, PhillyForce DUG Leader

@AmiAssayag

Q & A