You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function部署后调用第三方API失败 本地调试运行正常

Azure Function部署后调用第三方API返回微软登录页问题

问题表现

  • 本地环境验证全流程正常:VS Code本地调试模式下,调用第三方token端点可正常获取令牌,携带令牌调用业务端点返回符合预期的响应;Postman直接测试接口也可得到正确返回
  • 部署到Azure Function App后请求全失败:无论直接调用token获取接口,还是传入从Postman复制的硬编码有效token调用第二个业务接口,均无法得到正常响应
  • 函数触发逻辑正常:运行日志可确认函数被正常唤起,但接口返回内容为大段HTML文本,核心标识为包含Sign in to your account的登录页内容,而非预期的JSON格式返回
  • 已完成基础校验:确认请求传入的grant_type、client_Id、client_secret、username、password、scope等参数值配置均正确,初步怀疑第三方API拦截或限制了来自Azure Function的请求,经多轮排查未定位根因

相关代码

GetToken方法实现

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

日志信息

响应头日志(Log1)

响应状态码显示200 OK,但响应头标识返回内容类型为text/html,响应归属微软AAD登录服务

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

响应内容日志(Log2)

返回HTML为微软账号登录页,仅可识别出“Sign in to your account”字样,无对应登录交互入口,大段内容已省略

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=code\u0026client_id=5xxx2-085c-xxxx-bf88-xxxxx8\u0026scope=openid+profile+email+offline_access\u0026response_mode=form_post\u0026redirect_uri=https%3a%2f%2flogin.microsoftonline.com%2fcommon%2ffederation%2foauth2msa\u0026state=rQQIxxxxxxFM4lbVARUISEBAuDmwnlYp99Z8dGDEFtIWogJQGEUCQ4n-_SE845sZ2UgBj5GZkYChISYycEC3QBJIREp0wIMSPBABJbO-KUFYnlLe99f-87Wxxxxkk1A8g9i0b-pawoLCQ6buGcLDlxEf2H_549xxxxvH95ugLnroRzxCot6m-DEWpr2E0_Xk7HicVcOaIUPhlJF8XSvvwZgAsAPADbzJYdaFAufQMQtA2LmBpASIaAgriAkEAjR6tf8fLM2TNfM6YhieZtvFB7ka_3-ahzdGrdTmnLvjlZXIxrK4FJ0k6sWT-Ox5qlhGJa1enI-iYRPNU_QMOFlrRnLrlQ0bNH1y3Goedpfux2982_DHb2puAy4SmU67ugsUoqztKOnUxxxxx4PMkZs0Sp1GIaYZy_EJnGgbwgTWowz5mAfE-FmyDZPEhmpPYiDGEKE2dCx7QzCmAt9GhjQtlwR2Dwj84V2d2FJBU2xl3mVxrS38LJQsijF1LQI9G1uQlwNCHSRS6AjmDBIpuX6xnahGPW5ksGXApjMgN0Z8Hw26_H4fPfYz1NXlp6IsnPjXju3PasnA0MuXsNdtFLt9thyO0zZsoqvxiuN9bONOg4vLEbDxsUzdnVQO4089KgIfhfBw325rbn_1P7iAHh3MLd7aOfbqzf3nz7-de4P0\u0026estsfed=1\u0026uaid=71c115c676664cc9bad0639fd6e7c4bf\u0026signup=1\u0026lw=1\u0026fl=easi2\u0026fci=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=code\u0026client_id=xxx-085c-xx-bf88-xxx\u0026scope=openid+profile+email+offline_access\u0026response_mode=form_post\u0026redirect_uri=https%3a%2f%2flogin.microsoftonline.com%2fcommon%2ffederation%2foauth2msa\u0026state=rQQIAxxxVE9bNNAxxxISEBAuDmwnlYp99Z8dGDEFtIWogJQGEUCQ4n-_SE845sZ2UgBj5GZkYChISYycEC3QBJIREp0wIMSPBABJbO-KUFYnlLe99f-87WUAV5JUc7gSUVTkxxxx-pawoLCQ6buGcLDlxEf2H_549P1AjD41tibs2efvH95ugLnroRzxCot6m-DEWpr2E0_Xk7HicVcOaIUPhlJF8XSvvxxxbzJYdaFAxxxxxiAkEAjR6tf8fLM2TNfM6YhieZtvFB7ka_3-ahzdGrdTmnLvxxxrK4FJ0k6sWT-Ox5qlhGJa1enI-iYRPNU_QMOFlrRnLrlQ0bNH1y3Goedpfux2982_DHb2puAy4SmU67ugsUoqztKOnUymtrLWyW56kmRshQ14PMkZs0Sp1GIaYZy_EJnGgbwgTWowz5mAfE-FmyDZPEhmpPYiDGEKE2dCx7QzCmAt9GhjQtlwR2xxxxd2FJBU2xl3mVxrS38LJQsijF1LQI9G1uQlwNCHSRS6AjmDBIpxxxxsGXApjMxxx26_H4fPfYz1NXlp6IsnPjXju3PasnA0MuXsNdxxxvxiuN9bONOg4vLEbDxsUzdnVQO4089KgIfhfBw325rbn_1P7iAHh3MLd7aOfbqzf3nz7-de4P0\u0026estsfed=1\u0026uaid=71c115c676664cc9bad0639fd6e7c4bf\u0026fci=7e7dxxxx-b3f3-f312b90f7437\u0026idp_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!!!
---------
-- 以上为大段HTML内容,后续内容省略 ---

异常日志

异常为代码尝试将HTML响应解析为JSON格式时抛出,无额外根因参考价值

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.)

求助

遇到过同类问题的开发者可反馈可能的故障原因,当前仅通过代码较难直接定位问题。


内容的提问来源于stack exchange,提问作者sach2320

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 21:54:34