我想集成一个驱动器(个人和企业),为此我使用msal。我发现它需要镀铬。我想在我的应用程序中不使用chrome(如弹出窗口或应用程序UI)。
我正在使用此库进行身份验证(msal
)https://github.com/AzureAD/microsoft-authentication-library-for-android
有什么解决方案可用于Android原生
MSAL.Android 0.2.0的发布包括:
支持Android上的所有主流浏览器
MSAL库需要支持自定义选项卡的浏览器。 Chrome确实如此。 code comments from the MSAL AuthenticationActivity class告诉我们,只有安装了Chrome,活动才会启动。否则抛出异常。该库是开源的,因此您可以分叉存储库并修改AuthenticationActivity类以满足您的要求。
我使用webview加载到url下面 -
这将返回代码
应该用于获取访问令牌如下 -
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请求的响应将返回访问令牌。
如果有人仍然遇到此问题,当前版本的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过滤器。