OneDrive / onedrive-sdk-csharp

OneDrive SDK for C#! https://dev.onedrive.com
Other
295 stars 145 forks source link

Cannot Accessing OneDrive with User Token (RefreshToken) #221

Open gjohncock opened 7 years ago

gjohncock commented 7 years ago

I need to set up OneDrive to stay logged in so after much researching and testing I have arrived at the following code. Everything works up to the point of accessing the OneDrive drive (code line that reads myDrv = Await odc.Drive.Request.GetAsync). The Stack Trace shows that the token is assigned successfully however i get the following error...

`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 `

Here is the code...

`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

' This is the URL the application will authenticate at.
Const authString As String = "https://login.windows.net/0a6154a1-...."

' The Azure AD Graph API is the "resource" we're going to request access to.
Const resAzureGraphAPI As String = "https://graph.windows.net"

' The Azure AD Graph API for my directory is available at this URL.
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, clientSecret, AadReturnUrl, authContext)
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)

' Instantiate an AuthenticationContext for my directory (see authString above).
Dim authenticationContext As New AuthenticationContext(authString, False)

' Create a ClientCredential that will be used for authentication.
' This is where the Client ID and Key/Secret from the Azure Management Portal is used.
Dim clientCred As New ClientCredential(clientId, clientSecret)

' Acquire an access token from Azure AD to access the Azure AD Graph (the resource)
' using the Client ID and Key/Secret as credentials.
Dim authenticationResult As AuthenticationResult = Await authenticationContext.AcquireTokenAsync(resAzureGraphAPI, clientCred)

' Return the access token.
Return authenticationResult.AccessToken

End Function `

daboxu commented 7 years ago

hi @gjohncock the adalAuthProvider is used for old pattern to get resourceId by DiscoveryService and then passing the refresh token and serviceResourceId in. For you as I saw you are using graph API, I would suggest you start with the new Graph SDK stack.

gjohncock commented 7 years ago

great, thanks for the direction. i will look into this...