我已经创建了一个azure http触发器(也尝试了定时器触发器)函数,它正在调用第三方API与必要的参数作为内容头。在VS Code本地(调试模式),它工作得很好。我正在获得所需的响应(如果我们调用令牌端点以及通过传递令牌调用另一个端点的适当响应,则获得令牌)。即使有邮差,我也能得到正确的回应。
但是当我将这个函数部署到azure函数应用程序时,它没有从两个端点得到响应。(无论是从令牌1,还是将从邮差复制的硬编码令牌传递到第二个端点)。
在日志中,我可以看到函数被调用,当我记录响应时,我得到了一个大的html文本(不知道那是什么)是否有可能第三方API阻止或限制azure函数的调用(尽管如果我们传递适当的值,它可以在本地或与postman一起工作)什么都试过了,就是找不到确切的问题。我已经验证我传递正确的值grant_type, client_Id, client_secret, username, password, scope)
已将示例代码粘贴到下面:
private async Task<Token> GetToken()
{
try{
HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type", Environment.GetEnvironmentVariable("test-grant-type")},
{"username", Environment.GetEnvironmentVariable("test-username")},
{"password", Environment.GetEnvironmentVariable("test-password")},
{"client_id", Environment.GetEnvironmentVariable("test-client-id")},
{"client_secret", Environment.GetEnvironmentVariable("test-client-secret")},
{"scope", Environment.GetEnvironmentVariable("test-scope")}
});
HttpClient client = new HttpClient();
string tokenEndPoint = Environment.GetEnvironmentVariable("test-tokenApiEndPoint");
//calling API
HttpResponseMessage messageResult = await client.PostAsync(tokenEndPoint, content);
_logger.LogInformation($"Log Info: GetToken method: httpResponseMsg: {messageResult}");
string apiResponse = messageResult.Content.ReadAsStringAsync().Result;
_logger.LogInformation($"Log Info: GetToken method: apiResponse: {apiResponse}");
Token resTokenModel = JsonConvert.DeserializeObject<Token>(apiResponse);
}
catch (System.Exception ex)
{
_logger.LogError($"Exception : GetToken Method: Exception: {ex.Message}");
throw new Exception(ex.Message);
}
return resTokenModel;
}
日志html响应样例(它不完整,因为它非常大),Log1:
2022-06-29T08:30:05.490 [Information] Log Info: GetToken method: httpResponseMsg: StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:{Cache-Control: no-store, no-cachePragma: no-cacheStrict-Transport-Security: max-age=31536000; includeSubDomainsX-Content-Type-Options: nosniffX-Frame-Options: DENYLink: <https://aadcdn.msauth.net>; rel=preconnect; crossoriginLink: <https://aadcdn.msauth.net>; rel=dns-prefetchLink: <https://aadcdn.msftauth.net>; rel=dns-prefetchX-DNS-Prefetch-Control: onP3P: CP="DSP CUR OTPi IND OTRi ONL FIN"x-ms-request-id: 18670f19-3bec-4556-9b66-2c3760855300x-ms-ests-server: 2.1.13006.6 - WEULR1 ProdSlicesX-XSS-Protection: 0Set-Cookie: buid=0.AQIANaKxxxx_PBbRZsxxxx_PzErkPdDcCAdAA.xxxx--DLA3VO7QrddgJg7WevrDRjJU0T4KWPoylW2bC-ExpJHweIFWSInAdxjVwFITAQNF_FKbJPKHxxxxxp9ivEPYDacemz4wUo_sc04zyJbXjfz-2bVvxUz-AgAA; expires=Thu, 28-Jul-2022 08:30:05 GMT; path=/; secure; HttpOnly; SameSite=NoneSet-Cookie: fpc=AsZ-7OJNXMJAsZa7WVUJsyzxSauWAQAAAAyzTNoOAAAA; expires=Thu, 29-Jul-2022 08:30:05 GMT; path=/; secure; HttpOnly; SameSite=NoneSet-Cookie: esctx=AQABAAAAAAD--DLA3VO7QrddgJg7WevrvlcjjHs6JSFKoM9RHgIe1sGicQYOARxxxxwtTCtFHUZ7DawxrkQC4x3uxxkXSFkTTUgthZA1MEqk-Pmv2mzfkL9xBHg1V-LSzxeaWxtB38SA--OJKRBbdBVRR0D80RGqwGWTn5bm4XeaaWFCt5SYYet_q1fuHwxUMe7OXIuQgAA; domain=.login.microsoftonline.com; path=/; secure; HttpOnly; SameSite=NoneSet-Cookie: x-ms-gateway-slice=estsfd; path=/; secure; httponlySet-Cookie: stsservicecookie=estsfd; path=/; secure; httponlyDate: Tue, 28 Jun 2022 08:30:04 GMTContent-Type: text/html; charset=utf-8Expires: -1Content-Length: 195652}
从下面的html,我只能理解"登录到您的帐户"但在哪里签名,Log2:
2022-06-28T08:30:05.492 [Information] Log Info: Synergi Repo: GetToken method: apiResponse:<!-- Copyright (C) Microsoft Corporation. All rights reserved. --><!DOCTYPE html><html dir="ltr" class="" lang="en"><head><title>Sign in to your account</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Expires" content="-1"><link rel="preconnect" href="https://aadcdn.msauth.net" crossorigin><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="//aadcdn.msauth.net"><link rel="dns-prefetch" href="//aadcdn.msftauth.net"><meta name="PageID" content="ConvergedSignIn" /><meta name="SiteID" content="" /><meta name="ReqLC" content="1033" /><meta name="LocLC" content="en-US" /><meta name="referrer" content="origin" /><meta name="format-detection" content="telephone=no" /><noscript><meta http-equiv="Refresh" content="0; URL=https://login.microsoftonline.com/jsdisabled" /></noscript><meta name="robots" content="none" /><script type="text/javascript">//<![CDATA[$Config={"fShowPersistentCookiesWarning":false,"urlMsaSignUp":"https://login.live.com/oauth20_authorize.srf?response_type=codeu0026client_id=5xxx2-085c-xxxx-bf88-xxxxx8u0026scope=openid+profile+email+offline_accessu0026response_mode=form_postu0026redirect_uri=https%3a%2f%2flogin.microsoftonline.com%2fcommon%2ffederation%2foauth2msau0026state=rQQIxxxxxxFM4lbVARUISEBAuDmwnlYp99Z8dGDEFtIWogJQGEUCQ4n-_SE845sZ2UgBj5GZkYChISYycEC3QBJIREp0wIMSPBABJbO-KUFYnlLe99f-87Wxxxxkk1A8g9i0b-pawoLCQ6buGcLDlxEf2H_549xxxxvH95ugLnroRzxCot6m-DEWpr2E0_Xk7HicVcOaIUPhlJF8XSvvwZgAsAPADbzJYdaFAufQMQtA2LmBpASIaAgriAkEAjR6tf8fLM2TNfM6YhieZtvFB7ka_3-ahzdGrdTmnLvjlZXIxrK4FJ0k6sWT-Ox5qlhGJa1enI-iYRPNU_QMOFlrRnLrlQ0bNH1y3Goedpfux2982_DHb2puAy4SmU67ugsUoqztKOnUxxxxx4PMkZs0Sp1GIaYZy_EJnGgbwgTWowz5mAfE-FmyDZPEhmpPYiDGEKE2dCx7QzCmAt9GhjQtlwR2Dwj84V2d2FJBU2xl3mVxrS38LJQsijF1LQI9G1uQlwNCHSRS6AjmDBIpuX6xnahGPW5ksGXApjMgN0Z8Hw26_H4fPfYz1NXlp6IsnPjXju3PasnA0MuXsNdtFLt9thyO0zZsoqvxiuN9bONOg4vLEbDxsUzdnVQO4089KgIfhfBw325rbn_1P7iAHh3MLd7aOfbqzf3nz7-de4P0u0026estsfed=1u0026uaid=71c115c676664cc9bad0639fd6e7c4bfu0026signup=1u0026lw=1u0026fl=easi2u0026fci=7e7dac8e-5abd-4b36-b3f3-f312b90f7437","urlMsaLogout":"https://login.live.com/logout.srf?iframed_by=https%3a%2f%2flogin.microsoftonline.com","urlOtherIdpForget":"https://login.live.com/forgetme.srf?iframed_by=https%3a%2f%2flogin.microsoftonline.com","showCantAccessAccountLink":true,"urlGitHubFed":"https://login.live.com/oauth20_authorize.srf?response_type=codeu0026client_id=xxx-085c-xx-bf88-xxxu0026scope=openid+profile+email+offline_accessu0026response_mode=form_postu0026redirect_uri=https%3a%2f%2flogin.microsoftonline.com%2fcommon%2ffederation%2foauth2msau0026state=rQQIAxxxVE9bNNAxxxISEBAuDmwnlYp99Z8dGDEFtIWogJQGEUCQ4n-_SE845sZ2UgBj5GZkYChISYycEC3QBJIREp0wIMSPBABJbO-KUFYnlLe99f-87WUAV5JUc7gSUVTkxxxx-pawoLCQ6buGcLDlxEf2H_549P1AjD41tibs2efvH95ugLnroRzxCot6m-DEWpr2E0_Xk7HicVcOaIUPhlJF8XSvvxxxbzJYdaFAxxxxxiAkEAjR6tf8fLM2TNfM6YhieZtvFB7ka_3-ahzdGrdTmnLvxxxrK4FJ0k6sWT-Ox5qlhGJa1enI-iYRPNU_QMOFlrRnLrlQ0bNH1y3Goedpfux2982_DHb2puAy4SmU67ugsUoqztKOnUymtrLWyW56kmRshQ14PMkZs0Sp1GIaYZy_EJnGgbwgTWowz5mAfE-FmyDZPEhmpPYiDGEKE2dCx7QzCmAt9GhjQtlwR2xxxxd2FJBU2xl3mVxrS38LJQsijF1LQI9G1uQlwNCHSRS6AjmDBIpxxxxsGXApjMxxx26_H4fPfYz1NXlp6IsnPjXju3PasnA0MuXsNdxxxvxiuN9bONOg4vLEbDxsUzdnVQO4089KgIfhfBw325rbn_1P7iAHh3MLd7aOfbqzf3nz7-de4P0u0026estsfed=1u0026uaid=71c115c676664cc9bad0639fd6e7c4bfu0026fci=7e7dxxxx-b3f3-f312b90f7437u0026idp_hint=github.com","fShowSignInWithGitHubOnlyOnCredPicker":true,"fEnableShowResendCode":true,"iShowResendCodeDelay":90000,"sSMSCtryPhoneData":"AF~Afghanistan~93!!!AX~Åland Islands~358!!!AL~Albania~355!!!Islands~358!!!AL~Albania~355!!!
---------
-- and so here is large html content as above ---
--------------------------------------------------------------------------------------------------异常日志:这是因为在代码中,我们正在解析响应(所以下面的问题是因为它试图解析响应html) -所以这似乎并不有用2022-06-29T15:18:14.657 [Error] Exception : GetToken Method: Exception: Unexpected character encountered while parsing value: '<'. Path '', line 0, position 0.
2022-06-29T15:18:14.710 [Error] Exception : Method: Exception: One or more errors occurred. (Unexpected character encountered while parsing value: '<'. Path '', line 0, position 0.)
如果有人遇到过类似的问题,或者可以给出一个想法,什么可能是原因,虽然很难找到仅仅看代码
感谢-
"解析值时遇到意外字符:'<'"等错误。"路径",行号0,位置0;几乎都是JSON解析错误。
-
发生这种情况的最常见原因是被反序列化的字符串实际上不是有效的JSON。
-
还检查包含JSON字符串的文件是否有[BOM -字节顺序标记]。尝试删除BOM以解决错误。
感谢所有回复和浏览此帖子的人。
我们已经发现了实际的问题,它不是来自代码/开发方面。供应商已经将API迁移到azure服务器,现在他们有了另一个级别的身份验证(可能是azure Ad)
所以我认为原始的身份验证(JWT Oauth)返回了它的令牌,但我们没有验证azure的,所以这可能是我们得到azure html页面的原因。
现在API团队正在寻找需要做什么。我们可以关闭这个线程,或者如果有人有任何解决方案或建议,他们可以分享。
感谢