WSO2 ESB - 使用 OAuth2 服务



>我正在调用OAuth令牌服务来检索令牌。 下面是我的代理。这是一个检索令牌的简单 rest 终结点调用。出于测试目的,我正在尝试在响应中记录令牌。

<?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

如果有人能指导我在这里做错了什么,将不胜感激。

我能够使用以下有效负载调用服务。

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

这有效并检索了令牌。

这是我在组件中使用的模板来设置 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>

最新更新