集成 Azure AD 以进行用户身份验证并使用 REST API 将数据存储在用户的一个驱动器上
Integrate Azure AD for user Authentication and store data on user's one-drive using REST API
我想将 Azure AD 服务与我的 Web 应用程序集成,以对用户进行身份验证并将文档存储到用户的单驱动器位置。
我尝试了 adal4j 库示例,我在 azure 门户上配置了我的应用程序并能够对其进行身份验证。但是现在我需要使用 Microsoft Graph API 来使用单驱动服务(上传文件)。
你们有什么建议吗?我检查了 Java 个示例不可用的图形 API。
我试过下面的库。
https://azure.microsoft.com/en-in/resources/samples/active-directory-java-webapp-openidconnect/
也在下面 link 中提到,我没有找到 Java 的任何样本。
https://docs.microsoft.com/en-us/onedrive/developer/rest-api/?view=odsp-graph-online
听起来您正在尝试使用 Java 进行 onedrive Graph API 调用。
您所指的 ADAL4J 库有一个显示如何使用它的 wiki。
ADAL4J github 库的 wiki 向您展示了如何获取访问令牌并调用 Microsoft Graph:https://github.com/AzureAD/azure-activedirectory-library-for-java/wiki/ADAL4J-Basics
以下摘录:
Here are the steps to get started with ADAL4J:
Instantiate the ADAL AuthenticationContext object.
String authority =
"https://login.microsoftonline.com/contoso.onmicrosoft.com/";
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority,
true, service); Use the authentication context instance to acquire
tokens. ADAL4J provides different methods to acquire tokens based on
your application type. Refer the acquire tokens section for the
appropriate method for your implementation.
Use the acquired token as a bearer token in the call to the web API.
Future future =
context.acquireTokenByAuthorizationCode(code, redirectUri, new
ClientCredential(clientId, clientSecret), null, null);
AuthenticationResult result = future.get();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " +
result.getAccessToken()); You can also refer this full sample of a web
app using ADAL4J to authenticate users and get tokens for the MS Graph
API.
获得访问令牌后,您将在此处遵循 OneDrive Graph API 的入门指南:https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/?view=odsp-graph-online
摘录如下:
- User authentication and authorizing your app Microsoft Graph and OneDrive API use OAuth 2.0 for authorization. By completing an OAuth
flow, your app receives an access token that provides access to the
Microsoft Graph a particular set of permissions for a user.
Your app provides the access token in each request, through an HTTP
header:
Authorization: bearer {token}
For more information on authorizing your application and obtaining an
access token, see App authorization with Microsoft Graph.
- Make calls to a resource Once your app is authorized and received an access token, it can make requests to the Microsoft Graph endpoint
for OneDrive or SharePoint resources. To construct the URL for a
resource, you need to know the relative URL for the root resource
(like a user, group, or site) and the drive resource or driveItem
resource your request is targeting.
A request URL includes these components:
Microsoft Graph root URL and version
(https://graph.microsoft.com/v1.0) A root resource target
(/users/{id}) A OneDrive API resource target (/drive or
/drives/{id}/items/{item-id} or /drive/root:/path/to/item) Note:
Throughout the documentation, only partial syntax such as: GET
/drive/items/{item-id} is used for the sake of brevity. Prefix the
path with the correct root URL and root resource target in order to
obtain the full resource path or URL.
如果您还有其他问题,请发表评论。
我想将 Azure AD 服务与我的 Web 应用程序集成,以对用户进行身份验证并将文档存储到用户的单驱动器位置。
我尝试了 adal4j 库示例,我在 azure 门户上配置了我的应用程序并能够对其进行身份验证。但是现在我需要使用 Microsoft Graph API 来使用单驱动服务(上传文件)。
你们有什么建议吗?我检查了 Java 个示例不可用的图形 API。
我试过下面的库。
https://azure.microsoft.com/en-in/resources/samples/active-directory-java-webapp-openidconnect/
也在下面 link 中提到,我没有找到 Java 的任何样本。
https://docs.microsoft.com/en-us/onedrive/developer/rest-api/?view=odsp-graph-online
听起来您正在尝试使用 Java 进行 onedrive Graph API 调用。
您所指的 ADAL4J 库有一个显示如何使用它的 wiki。
ADAL4J github 库的 wiki 向您展示了如何获取访问令牌并调用 Microsoft Graph:https://github.com/AzureAD/azure-activedirectory-library-for-java/wiki/ADAL4J-Basics
以下摘录:
Here are the steps to get started with ADAL4J:
Instantiate the ADAL AuthenticationContext object.
String authority = "https://login.microsoftonline.com/contoso.onmicrosoft.com/"; ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authority, true, service); Use the authentication context instance to acquire tokens. ADAL4J provides different methods to acquire tokens based on your application type. Refer the acquire tokens section for the appropriate method for your implementation.
Use the acquired token as a bearer token in the call to the web API.
Future future = context.acquireTokenByAuthorizationCode(code, redirectUri, new ClientCredential(clientId, clientSecret), null, null); AuthenticationResult result = future.get();
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Authorization", "Bearer " + result.getAccessToken()); You can also refer this full sample of a web app using ADAL4J to authenticate users and get tokens for the MS Graph API.
获得访问令牌后,您将在此处遵循 OneDrive Graph API 的入门指南:https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/?view=odsp-graph-online
摘录如下:
- User authentication and authorizing your app Microsoft Graph and OneDrive API use OAuth 2.0 for authorization. By completing an OAuth flow, your app receives an access token that provides access to the Microsoft Graph a particular set of permissions for a user.
Your app provides the access token in each request, through an HTTP header:
Authorization: bearer {token}
For more information on authorizing your application and obtaining an access token, see App authorization with Microsoft Graph.
- Make calls to a resource Once your app is authorized and received an access token, it can make requests to the Microsoft Graph endpoint for OneDrive or SharePoint resources. To construct the URL for a resource, you need to know the relative URL for the root resource (like a user, group, or site) and the drive resource or driveItem resource your request is targeting.
A request URL includes these components:
Microsoft Graph root URL and version (https://graph.microsoft.com/v1.0) A root resource target (/users/{id}) A OneDrive API resource target (/drive or /drives/{id}/items/{item-id} or /drive/root:/path/to/item) Note: Throughout the documentation, only partial syntax such as: GET /drive/items/{item-id} is used for the sake of brevity. Prefix the path with the correct root URL and root resource target in order to obtain the full resource path or URL.
如果您还有其他问题,请发表评论。