Flutter 登录后不会重定向 Azure API 请求

问题描述 投票:0回答:1

我正在尝试在 Flutter 应用程序中调用 Azure API。我使用 aad_oauth 包。 我成功登录并且可以获得 Microsoft 访问令牌,但是当我调用 GET API 将不记名令牌作为授权标头传递时,我无法获得预期的 JSON 响应。

这是aad_oauth配置:

static final Config config = Config(
tenant: '***',
clientId: '***',
scope: 'openid profile offline_access',
navigatorKey: Get.key,
redirectUri: '***',
loader: SizedBox());

static final AadOAuth oauth = AadOAuth(config);

LoginViewModel.dart

final result = await oAuth.login();
String accessToken = await oAuth.getAccessToken();

访问令牌已正确检索并存储。

这是 API 调用,使用 Dart dio:

  @override
  Future<ShipGroupModel?> getShips() async {
    try {
      final Response<dynamic> response = await _dio.get(
        "/api/ship",
      );
    } on DioError catch (e) {
      // exception handling
    }
}

但这是响应正文:

<!-- 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">//<... JS code here ...></script>
          
      </body>
      </html>

所以,我收到了 HTML 页面。 API 调用返回一个 200 状态代码和一个重定向 URL,如下所示:

https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize?client_id={client_id}&redirect_uri={redirect_uri}&client_info={client_info}

如何通过处理 Microsoft 重定向来获取 API 内容响应?

flutter azure dart oauth dio
1个回答
0
投票

还有另一种方法可以解决您的 Azure 身份验证问题。使用这个包代替

AadOAuth
azure_silent_auth

对于这个问题,应用程序在身份验证后没有重定向

. use the 
PCAuthenticator
instead of the
示例中提供的DefaultAuthenticator`。

PCAuthenticator
创建一个单独的窗口来加载身份验证屏幕,而不是将其重定向到浏览器。身份验证完成后,此新窗口也会关闭。如果您愿意,您可以修改它并创建您自己的自定义身份验证器并在
AzureAuth
构造函数中使用它。

成功登录后,您可以随时随地使用

AzureAuth->getAccessToken
获取访问令牌。如果令牌过期了,它甚至会为您刷新令牌。

此软件包默认安全地保存访问令牌,以便您下次使用

AzureAuth->silentLogin
进行静默登录。

希望这有帮助。

class AuthenticationHandler {
  static final AuthenticationHandler _instance =
      AuthenticationHandler._internal();

  factory AuthenticationHandler() {
    return _instance;
  }

  AuthenticationHandler._internal();

  // !! USE PCAuthenticator here instead of DefaultAuthenticator
  final AzureAuth _microsoftAuthenticator = AzureAuth(
    authenticatorProvider: DefaultAuthenticator(
      'https://login.microsoftonline.com/{tenant-id}/',
      ["openId", "offline_access"],
      '{client-id}',
      '&prompt=select_account',
      3000, // localhost redirect uri port
    ),
  );

  Future<void> login() async {
    await _microsoftAuthenticator.login();
  }

  Future<void> silentLogin() async {
    await _microsoftAuthenticator.silentLogin();
  }

  Future<void> logout() async {
    await _microsoftAuthenticator.logout();
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.