[統合]{class="badge positive"}

Marketo 認証サービス

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

Marketo の REST API は 2-legged OAuth 2.0 で認証が行われます。Marketo に対して認証するには、カスタム認証を作成する必要があります。通常、このカスタム認証は OSGi バンドル内に書き込まれています。次のコードは、このチュートリアルの一部として使用されたカスタム認証サービスを示しています。

カスタム認証サービス

次のコードでは、Marketo に対する認証に必要なアクセストークンを持つ AuthenticationDetails オブジェクトを作成します。

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 は IAuthentication インターフェイスを実装しています。このインターフェイスは、AEM Forms Client SDK に含まれています。このサービスはアクセストークンを取得し、そのトークンを AuthenticationDetails の HttpHeader に挿入します。AuthenticationDetails オブジェクトの HttpHeaders にデータが設定されると、AuthenticationDetails オブジェクトがフォームデータモデルの Dermis レイヤーに返されます。

getAuthenticationType メソッドで返される文字列に注意してください。この文字列は、データソースの設定時に使用されます。

アクセストークンの取得

access_token を返す 1 つのメソッドを持つシンプルなインターフェイスを定義します。このインターフェイスを実装するクラスのコードは、後で示されています。

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

次のコードは、REST API の呼び出し時に使用される access_token を返すサービスのコードです。このサービスのコードでは、GET 呼び出しに必要な設定パラメーターにアクセスします。このように、GET URL に client_id と client_secret を渡して、access_token を生成します。その後、この access_token は呼び出し元のアプリケーションに返されます。

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;
    }
}

設定の必要な設定プロパティを以下のスクリーンショットに示します。これらの設定プロパティは、上記のコードで読み取られ、access_token を取得します。

config

設定

設定プロパティの作成には、次のコードを使用しました。これらのプロパティは、お使いの Marketo インスタンスに固有です。

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 "";
}

次のコードでは、設定プロパティを読み取り、ゲッターメソッドを使用して同じ値を返しています。

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. バンドルをビルドし、AEM サーバーにデプロイします。
  2. ブラウザーで configMgr にアクセスし、「Marketo Credentials Service Configuration」を検索します。
  3. Marketo インスタンスに固有の適切なプロパティを指定します。

次の手順

RESTful サービスベースのデータソースの作成

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