[Integrering]{class="badge positive"}

Marketo autentiseringstjänst

[AEM Forms 6.5]{class="badge informative"}

Marketo REST API:er autentiseras med 2-legged OAuth 2.0. Vi måste skapa anpassad autentisering för att autentisera mot Marketo. Denna anpassade autentisering skrivs vanligtvis inuti ett OSGI-paket. Följande kod visar den anpassade autentiseraren som användes som en del av den här självstudiekursen.

Tjänst för anpassad autentisering

Följande kod skapar objektet AuthenticationDetails som har den åtkomsttoken som krävs för autentisering mot Marketo

package com.marketoandforms.core;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.adobe.aemfd.dermis.authentication.api.IAuthentication;
import com.adobe.aemfd.dermis.authentication.exception.AuthenticationException;
import com.adobe.aemfd.dermis.authentication.model.AuthenticationDetails;
import com.adobe.aemfd.dermis.authentication.model.Configuration;
@Component(service={IAuthentication.class}, immediate=true)
public class MarketoAuthenticationService implements IAuthentication {
@Reference
MarketoService marketoService;
    @Override
    public AuthenticationDetails getAuthDetails(Configuration arg0) throws AuthenticationException
    {
        AuthenticationDetails auth = new AuthenticationDetails();
        auth.addHttpHeader("Cache-Control", "no-cache");
        auth.addHttpHeader("Authorization", "Bearer " + marketoService.getAccessToken());
        return auth
    }

    @Override
    public String getAuthenticationType() {
        // TODO Auto-generated method stub
        return "AemForms With Marketo";
    }
}

MarketoAuthenticationService implementerar IAuthentication-gränssnittet. Gränssnittet ingår i AEM Forms Client SDK. Tjänsten hämtar åtkomsttoken och infogar token i HttpHeader för AuthenticationDetails. När HttpHeaders för AuthenticationDetails-objektet har fyllts i returneras AuthenticationDetails-objektet till Dermis-lagret för formulärdatamodellen.

Var uppmärksam på den sträng som returneras av metoden getAuthenticationType. Den här strängen används när du konfigurerar datakällan.

Hämta åtkomsttoken

Ett enkelt gränssnitt definieras med en metod som returnerar access_token. Koden för den klass som implementerar det här gränssnittet visas längre ned på sidan.

package com.marketoandforms.core;
public interface MarketoService {
    String getAccessToken();
}

Följande kod tillhör tjänsten som returnerar den access_token som ska användas för REST API-anrop. Koden i den här tjänsten har åtkomst till de konfigurationsparametrar som behövs för att ringa GETEN. Som du ser skickar vi client_id,client_secrets i GET-URL:en för att generera access_token. Denna access_token returneras sedan till det anropande programmet.

package com.marketoandforms.core.impl;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.marketoandforms.core.*;
@Component(service=MarketoService.class,immediate = true)
public class MarketoServiceImpl implements MarketoService {
    private final Logger log = LoggerFactory.getLogger(getClass());
@Reference
MarketoConfigurationService config;
    @Override
    public String getAccessToken()
    {
        String AUTH_URL = config.getAUTH_URL();
        String CLIENT_ID = config.getCLIENT_ID();
        String CLIENT_SECRET = config.getCLIENT_SECRET();
        String AUTH_PATH = config.getAUTH_PATH();
        HttpClient httpClient = HttpClientBuilder.create().build();
        String getURL = AUTH_URL+AUTH_PATH+"&client_id="+CLIENT_ID+"&client_secret="+CLIENT_SECRET;
        log.debug("The url to get the access token is "+getURL);
        HttpGet httpGet = new HttpGet(getURL);
        httpGet.addHeader("Cache-Control","no-cache");
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            org.json.JSONObject responseJSON = new org.json.JSONObject(EntityUtils.toString(httpEntity))
            return (String)responseJSON.get("access_token");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

Skärmbilden nedan visar de konfigurationsegenskaper som behöver ställas in. Dessa konfigurationsegenskaper läses in i koden ovan för att få åtkomst_token

config

Konfiguration

Följande kod användes för att skapa konfigurationsegenskaperna. Dessa egenskaper är specifika för din Marketo-instans

package com.marketoandforms.core;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name="Marketo Credentials Service Configuration", description = "Connect Form With Marketo")
public @interface MarketoConfiguration {
     @AttributeDefinition(name="Identity Endpoint", description="URL of Marketo Identity Endpoint")
     String identityEndpoint() default "";
      @AttributeDefinition(name="Authentication path", description="Marketo authentication path")
      String authPath() default "";
      @AttributeDefinition(name="Client ID", description="Client ID")
      String clientID() default "";
      @AttributeDefinition(name="Client Secret", description="Client Secret")
      String clientSecret() default "";
}

Följande kod läser konfigurationsegenskaperna och returnerar samma via get-metoderna

package com.marketoandforms.core;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=true, service={MarketoConfigurationService.class})
@Designate(ocd=MarketoConfiguration.class)
public class MarketoConfigurationService {
    private final Logger log = LoggerFactory.getLogger(getClass());
    private MarketoConfiguration config;
    private String AUTH_URL;
    private String  AUTH_PATH;
    private String CLIENT_ID ;
    private String CLIENT_SECRET;
     @Activate
      protected final void activate(MarketoConfiguration config) {
        System.out.println("####In my marketo activating auth service");
        AUTH_URL = config.identityEndpoint();
        AUTH_PATH = config.authPath();
        CLIENT_ID = config.clientID();
        CLIENT_SECRET = config.clientSecret();
        log.info("clientID:" + CLIENT_ID);
        System.out.println("The client id is "+CLIENT_ID+"AUTH PATH"+AUTH_PATH);
      }
    public String getAUTH_URL() {
        return AUTH_URL;
    }
   public String getAUTH_PATH() {
        return AUTH_PATH;
    }
    public String getCLIENT_ID() {
        return CLIENT_ID;
    }

    public String getCLIENT_SECRET() {
        return CLIENT_SECRET;
    }
}
  1. Bygg och distribuera paketet på din AEM.
  2. Peka din webbläsare på configMgr och sök efter "Marketo Credentials Service Configuration"
  3. Ange lämpliga egenskaper för din Marketo-instans

Nästa steg

Skapa RESTful-tjänstbaserad datakälla

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e