Onedrive SDK VB.NET - 使用AAD的用户刷新令牌访问一个驱动器

时间:2017-03-10 03:28:01

标签: vb.net azure onedrive

我需要一个应用程序来设置OneDrive以保持登录状态,因此经过大量研究和测试后,我已经得到了以下代码。 我使用Microsoft Graph从Azure获取正确的用户刷新令牌,但似乎我尝试创建OneDriveClient对象的方式不正确。

一切正常,直到访问OneDrive驱动器。

堆栈跟踪显示令牌已成功分配但是我收到以下错误...

Exception thrown: 'Microsoft.Graph.ServiceException' in mscorlib.dll
Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 : 8/03/2017 1:33:22 AM: 6f67595b-... - AcquireTokenHandlerBase: === Token Acquisition finished successfully. An access token was retuned:
Access Token Hash: 6nRavv....
Refresh Token Hash: jGnzim1W6Cw...
Expiration Time: 8/03/2017 2:33:21 AM +00:00
User Hash: null

CFSOneDriveSync.vshost.exe Information: 0 : 8/03/2017 1:33:22 AM: 6f67595b-... - AcquireTokenHandlerBase: === Token Acquisition finished successfully. An access token was retuned:
Access Token Hash: 6nRavv....
Refresh Token Hash: jGnzim1W6Cw...
Expiration Time: 8/03/2017 2:33:21 AM +00:00
User Hash: null

这是代码......

Imports Microsoft.OneDrive.Sdk
Imports Microsoft.OneDrive.Sdk.Authentication
Imports Microsoft.Azure.ActiveDirectory.GraphClient
Imports Microsoft.IdentityModel.Clients.ActiveDirectory

Shared siteprefix As String = "/sites/dev"
Shared clientId As String = "dr44f435-...."
Shared clientSecret As String = "aQhnY6nJlqqaQXjU/UyQ...."
Shared odc As OneDriveClient
Shared myDrv As Drive
Shared TokenForApplication As String

Public Const AadReturnUrl As String = "urn:....0:oob"
Public Const AadTargetUrl As String = "https://domain.onmicrosoft.com"

Private Shared Async Function Connect() As Task

    Const authString As String = "https://login.windows.net/0a6154a1-...."

    Const resAzureGraphAPI As String = "https://graph.windows.net"

    Const serviceRootURL As String = "https://graph.windows.net/0a6154a1-...."

    Dim serviceRoot As New Uri(serviceRootURL)

    Dim adClient As New ActiveDirectoryClient(serviceRoot, Async Function() Await GetAppTokenAsync())

    Dim authContext As AuthenticationContext = New AuthenticationContext(authString, False)

    Dim userCred As UserCredential = New UserCredential("user@domain.com.au", "**************")

    Dim authResult As AuthenticationResult = Await authContext.AcquireTokenAsync(resAzureGraphAPI, clientId, userCred)

    TokenForApplication = authResult.RefreshToken

    Dim adalAuthProvider As AdalAuthenticationProvider = New AdalAuthenticationProvider(clientId, AadReturnUrl, authContext)

    odc = New OneDriveClient(AadTargetUrl + siteprefix + "/_api/v2.0", adalAuthProvider)

    Dim authTask As Task = adalAuthProvider.AuthenticateUserWithRefreshTokenAsync(TokenForApplication)

    myDrv = Await odc.Drive.Request.GetAsync          <-- ERROR RAISED HERE

End Sub

Private Shared Async Function GetAppTokenAsync() As Task(Of String)

    Dim authenticationContext As New AuthenticationContext(authString, False)

    Dim clientCred As New ClientCredential(clientId, clientSecret)

    Dim authenticationResult As AuthenticationResult = Await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred)

    Return authenticationResult.AccessToken

End Function

非常感谢任何帮助。

干杯

Greg J

0 个答案:

没有答案