Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Cannot Accessing OneDrive with User Token (RefreshToken) #221

Closed
gjohncock opened this issue Mar 8, 2017 · 3 comments
Closed

Cannot Accessing OneDrive with User Token (RefreshToken) #221

gjohncock opened this issue Mar 8, 2017 · 3 comments

Comments

@gjohncock
Copy link

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("[email protected]", "**************")

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
Copy link
Contributor

daboxu commented Mar 22, 2017

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
Copy link
Author

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

@baywet
Copy link
Member

baywet commented Oct 2, 2024

Thank you for reaching out and for your patience. This SDK is being officially deprecated. See #259 for more information

@baywet baywet closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants