我有一个API应用程序(比如计算器),我使用Portal在其上启用了AD身份验证。我在API管理服务中添加了Calculator API。现在我想要获取OAuth令牌来调用Calculator API。我读了这篇post
在上面的帖子中提到,首先要获得授权码,然后才能获得令牌。我已经做了所有的AAD申请&得到了管理员的同意,一切。
在APIM中,我编写了一个策略来获取授权码
<send-request mode="new" response-variable-name="responseObject" timeout="20" ignore-error="true"> <set-url>@("{{frontAuthServer}}?client_id={{clientId}}&response_type=code&redirect_uri={{FrontredirectUri}}&response_mode=query&resource={{scope}}")</set-url> <set-method>GET</set-method> </send-request> <return-response response-variable-name="existing response variable"> <set-status code="200" reason="OK" /> <set-header name="Content-Type" exists-action="override"> <value>application/json</value> </set-header> <set-body> @(new JObject(new JProperty("code",((IResponse)context.Variables["code"]).Body.As<JObject>())).ToString()) </set-body> </return-response>
但不幸的是,作为响应,授权代码将作为查询参数出现&我无法使用Policy检索它。所以我只想知道我的方向是否正确,如果是,那么如何从响应中检索查询参数?或者哪种方式才是正确的呢?我跟着mentioned here走了同样的路
但没那么走运。需要做什么设置吗?我是不是遗漏了什么?
发布于 2017-06-13 10:19:36
如果你的目标是让你的api在没有oauth令牌的情况下也能被调用,那么你就走在了正确的道路上。如果auth token作为查询参数出现,那么你从服务器得到的应该是带有location头的302响应。如下所示:
<send-request mode="new" response-variable-name="responseObject" timeout="20" ignore-error="true">
<set-url>@("{{frontAuthServer}}?client_id={{clientId}}&response_type=code&redirect_uri={{FrontredirectUri}}&response_mode=query&resource={{scope}}")</set-url>
<set-method>GET</set-method>
</send-request>
<set-variable name="token" value="@{
var location = ((IResponse)responseObject).Headers.GetValueOrDefault("Location");
if (string.IsNullOrEmpty(location)) {
return null;
}
var tokenStart = location.IndexOf("token=");
if (tokenStart >= 0) {
tokenStart += 6;
var tokenEnd = location.IndexOf("&", tokenStart);
if (tokenEnd < 0) {
tokenEnd = location.Length;
}
return location.Substring(tokenStart, tokenEnd - tokenStart);
}
return null;
}" />在此之后,token应该在名为"token“的变量中,在策略表达式中可作为context.Variables"token”访问。
https://stackoverflow.com/questions/44499757
复制相似问题