2016-12-20 177 views
1

我使用客戶端ID和密碼進行身份驗證。Microsoft Graph API內容401未授權

String url = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenantContext); 
AuthenticationContext context = new AuthenticationContext(url, true, ForkJoinPool.commonPool()); 
AuthenticationResult result = context.acquireToken("https://graph.microsoft.com", new ClientCredential(clientId, clientSecret), null).get();  
String token = result.getAccessTokenType() + " " + result.getAccessToken(); 

我的應用程序都打勾許可箱和上述令牌我可以列出用戶和他們的穿越驅動器和文件夾。我可以訪問 https://graph.microsoft.com/v1.0/drives/%s/items/%s/content中的內容,它會在位置標題中返回另一個網址。但是,當我嘗試獲取該URL時,它返回401 Unauthorized。

+0

或者,項目json包含以下格式的字段: '「@ microsoft.graph.downloadUrl」:「https:// {tenant} -my.sharepoint.com/personal/ {user} _ {tenant} _onmicrosoft_com/_layouts/15/download.aspx?UniqueId = ...&access_token = ...&prooftoken = ...「' 看起來好像你應該能夠訪問但不起作用。 – Alastair

回答

0

graph.microsoft.com上的其他URL是否也是?如果沒有,那麼您需要爲該URL獲取新的身份驗證令牌,然後在您的下載請求中使用它。

就像你已經與graph.microsoft.com做,但與其他服務器名稱:

AuthenticationResult result = context.acquireToken("https://onedrive-server-name", new ClientCredential(clientId, clientSecret), null).get();  
+0

我嘗試了位置URL資源的一個令牌 - 「https:// {tenant} -my.sharepoint.com」,但仍獲得401.在「https:// manage.windowsazure.com」中,我添加了Graph,O365 Sharepoint在線和Azure AD到我的應用程序的所有讀取權限。 – Alastair

0

確保您已經添加以下權限: Files.ReadAll或Files.ReadWriteAll 吊銷應用程序訪問並再試一次(https://portal.office.com/account/#apps

但是,「@ microsoft.graph.downloadUrl」應該工作不正常,也許你禁用了庫設置中的離線訪問?

+0

我無法使用'ClientCredential','AsymmetricKeyCredential'或甚至使用完全登錄的刷新令牌下載。我得到'@ microsoft.graph.downloadUrl',但後來得到了401. 哪裏可以禁止下載? – Alastair

0

我成功地下載來自用戶的內容通過以管理員用戶登錄並獲得來自app authentication using Azure AD

獲得刷新令牌刷新令牌可以使用adal4j贖回:

AuthenticationContext context = new AuthenticationContext(url, true, ForkJoinPool.commonPool()); 
AuthenticationResult result = context.acquireTokenByRefreshToken(refreshToken, new ClientCredential(clientId, clientSecret), "https://graph.microsoft.com", null).get(); 
String token = result.getAccessTokenType() + " " + result.getAccessToken(); 

使用此訪問令牌您可以通過以下方式獲取文件內容: https://graph.microsoft.com/v1.0/drives/{driveid}/items/{itemid}/content

有一個重要的額外步驟 - 管理員用戶必須添加爲坐e收藏所有者,用於您正在訪問其內容的用戶。這在this blog中描述。