2017-08-16 180 views
2

我打算調用OAuth令牌服務來檢索令牌。 以下是我的代理。這是一個簡單的休息端點調用,用於檢索令牌。爲了測試目的,我試圖在響應中記錄令牌。WSO2 ESB - 使用OAuth2服務

<?xml version="1.0" encoding="UTF-8"?> 
<proxy name="sla_proxy_svc_vo2" startOnLoad="true" trace="disable" 
transports="http https" xmlns="http://ws.apache.org/ns/synapse"> 
<target> 
    <inSequence> 
     <log level="custom"> 
      <property name="msg" value="*****INITIATING*****" /> 
     </log> 
     <payloadFactory media-type="xml"> 
      <format> 
       <soapenv:Envelope xmlns:echo="http://echo.services.core.carbon.wso2.org" 
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
        <soapenv:Body> 
         <nstxt:text xmlns:nstxt="http://ws.apache.org/commons/ns/payload">grant_type=client_credentials&amp;client_id=G6Dk_3ZdrXOfPiuctufVq6GfTWoa&amp;client_secret=jxA8NTkEClE5xGUvGvvhVTDyXM4a</nstxt:text> 
        </soapenv:Body> 
       </soapenv:Envelope> 
      </format> 
      <args /> 
     </payloadFactory> 
     <log level="custom"> 
      <property name="msg" value="*****BEFORE TOKEN SERVICE CALL*****" /> 
     </log> 
     <log level="full" /> 
     <property name="HTTP_METHOD" scope="axis2" type="STRING" 
      value="POST" /> 
     <property name="messageType" scope="axis2" type="STRING" 
      value="text/plain" /> 
     <property name="ContentType" scope="axis2" type="STRING" 
      value="text/plain" /> 
     <property name="Accept" scope="axis2" type="STRING" 
      value="application/json" /> 
     <send> 
      <endpoint> 
       <http format="rest" method="post" trace="disable" 
        uri-template="http://10.236.70.9:8281/token" /> 
      </endpoint> 
     </send> 
    </inSequence> 
    <outSequence> 
     <log level="custom"> 
      <property name="msg" value="******OUT SEQUENCE*******" /> 
     </log> 
     <log level="full" /> 
     <send /> 
    </outSequence> 
    <faultSequence /> 
</target> 
</proxy> 

我在打電話時收到下面的回覆。

DEBUG {org.apache.synapse.transport.http.wire} - << "HTTP/1.1 415 Unsupported Media Type[\r][\n]" {org.apache.synapse.transport.http.wire} 
DEBUG {org.apache.synapse.transport.http.wire} - << "X-Frame-Options: DENY[\r][\n]" {org.apache.synapse.transport.http.wire} 
DEBUG {org.apache.synapse.transport.http.wire} - << "X-XSS-Protection: 1; mode=block[\r][\n]" {org.apache.synapse.transport.http 

公司將請如果有人能指導我的,我在這裏做什麼錯事。

+0

您好@Yasothar是您的問題修復,答案之一幫助你? – Nicolas

+0

是的尼古拉斯是有幫助的。我能找到它,你的答案也有幫助。 – Yasothar

回答

0

我能夠用以下有效載荷調用服務。

<payloadFactory media-type="xml"> 
      <format> 
       <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
        <soapenv:Body> 
         <root> 
          <grant_type>client_credentials</grant_type> 
          <client_id>G6Dk_3ZdrXOfPiuctufVq6GfTWoa</client_id> 
          <client_secret>jxA8NTkEClE5xGUvGvvhVTDyXM4a</client_secret> 
         </root> 
        </soapenv:Body> 
       </soapenv:Envelope> 
      </format> 
      <args /> 
     </payloadFactory> 

還必須添加內容類型如下。

<property name="messageType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded" /> 
<property name="ContentType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded" /> 

這工作和檢索的令牌。

0

這裏是我使用的組件設置OAuth憑證模板,你也許可以適應它一下你的情況下(聽起來像你不需要grantType或用戶憑證)

<?xml version="1.0" encoding="UTF-8"?> 
 
<template name="getToken" xmlns="http://ws.apache.org/ns/synapse"> 
 
    <parameter name="tokenURL"/> 
 
    <parameter name="clientId"/> 
 
    <parameter name="clientSecret"/> 
 
    <parameter name="grantType"/> 
 
    <sequence> 
 
     <property description="Base64 crendetials" expression="base64Encode(fn:concat($func:clientId,':',$func:clientSecret))" name="credentials" scope="default" type="STRING"/> 
 
     <property description="Authentication" expression="fn:concat('Basic ', get-property('credentials'))" name="Authorization" scope="transport" type="STRING"/> 
 
     <header name="Content-Type" scope="transport" value="application/x-www-form-urlencoded"/> 
 
     <property name="messageType" scope="axis2" type="STRING" value="application/x-www-form-urlencoded"/> 
 
     <property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/> 
 
     <property expression="$func:tokenURL" name="uri.var.authUrl" scope="default" type="STRING"/> 
 
     <property expression="$func:grantType" name="uri.var.grantType" scope="default" type="STRING"/> 
 
     <call blocking="true"> 
 
      <endpoint> 
 
       <http method="post" uri-template="{uri.var.authUrl}?grant_type={uri.var.grantType}"/> 
 
      </endpoint> 
 
     </call> 
 
     <property expression="json-eval($.access_token)" name="OAuth_Token" scope="default" type="STRING"/> 
 
     <property action="remove" description="Remove Headers" name="TRANSPORT_HEADERS" scope="axis2"/> 
 
     <property description="Authorization" expression="fn:concat('Bearer ',get-property('OAuth_Token'))" name="Authorization" scope="transport" type="STRING"/> 
 
    </sequence> 
 
</template>