没有使用chrome的Android的MSAL

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

我想集成一个驱动器(个人和企业),为此我使用msal。我发现它需要镀铬。我想在我的应用程序中不使用chrome(如弹出窗口或应用程序UI)。

我正在使用此库进行身份验证(msalhttps://github.com/AzureAD/microsoft-authentication-library-for-android

有什么解决方案可用于Android原生

android google-chrome oauth-2.0 onedrive msal
4个回答
2
投票

MSAL.Android 0.2.0的发布包括:

支持Android上的所有主流浏览器

https://github.com/AzureAD/microsoft-authentication-library-for-android/wiki/Migrating-from-MSAL.Android-0.1.n-to-0.2.0


0
投票

MSAL库需要支持自定义选项卡的浏览器。 Chrome确实如此。 code comments from the MSAL AuthenticationActivity class告诉我们,只有安装了Chrome,活动才会启动。否则抛出异常。该库是开源的,因此您可以分叉存储库并修改AuthenticationActivity类以满足您的要求。


0
投票

我使用webview加载到url下面 -

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=your_client_id&scope=offline_access+Files.ReadWrite+profile+openid+User.ReadBasic.All&redirect_uri=your_redirect_uri&prompt=login&response_type=code

这将返回代码

应该用于获取访问令牌如下 -

 private void requestForGetAccessToken(String code) {
    try {

        String bodyForAccessTokenRequest = getBodyForAccessTokenRequest(code);
         private static final String AUTHORITY = "https://login.microsoftonline.com/common/";
         private static final String URL_FOR_TOKEN = "oauth2/v2.0/token/" 
        URL mUrl = new URL(AUTHORITY + URL_FOR_TOKEN);
        HttpURLConnection httpConnection = (HttpURLConnection) mUrl.openConnection();
        httpConnection.setRequestMethod("POST");
        httpConnection.setDoOutput(true);
        httpConnection.setRequestProperty("Content-length", "" + bodyForAccessTokenRequest.length());
        httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpConnection.setUseCaches(false);
        httpConnection.setAllowUserInteraction(false);
        httpConnection.setConnectTimeout(100000);
        httpConnection.setReadTimeout(100000);

        OutputStream os = httpConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
        writer.write(bodyForAccessTokenRequest);
        writer.flush();
        writer.close();
        os.close();

        httpConnection.connect();

        int responseCode = httpConnection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            String strRes = sb.toString();
            sendCode(strRes);
        }
    } catch (IOException e) {
        Logger.e("[OneDrivePersonalLoginManager][requestForGetAccessToken] IOException " + e.getMessage());
    } catch (Exception e) {
        Logger.e("[OneDrivePersonalLoginManager][requestForGetAccessToken] Exception " + e.getMessage());
    }
}


 private String getBodyForAccessTokenRequest(String code) {
    return CLIENT_ID_EQUAL + CLIENT_ID + appendAmpersand() + appendRedirectUri() + appendAmpersand() + CODE_EQUAL + code + appendAmpersand() + GRANT_TYPE_EQUAL + AUTHORIZATION_CODE;
}

此POST请求的响应将返回访问令牌。


0
投票

如果有人仍然遇到此问题,当前版本的MSAL for Android允许您使用WebView而不是浏览器。如果您使用与使用auth_config.json文件进行应用配置的示例应用程序相同的实现,只需将授权用户代理更改为webview即可。

auth_config.json

{
  "client_id" : "<CLIENT_ID_FROM_https://apps.dev.microsoft.com>",
  "authorization_user_agent" : "WEBVIEW", 
  "redirect_uri" : "<CLIENT_ID_FROM_https://apps.dev.microsoft.com>://auth",
  "authorities" : [
   {
      "type": "AAD",
      "audience": {
         "type": "AzureADandPersonalMicrosoftAccount"
       }
   }
 ]
}

如果您将使用此路由,则不需要使用先前实现中的Intent过滤器。

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