OneDrive / onedrive-sdk-dotnet-msa-auth-adapter

Other
26 stars 22 forks source link

Failed to acquire token silently. Call method AcquireToken #13

Closed huorswords closed 7 years ago

huorswords commented 7 years ago

Hi OneDrive team!

I don't know whether this project is where to ask about our issue. If it's not Please consider to redirect me to the appropriate team.

With the objetive of establishing a OneDrive Business connection to retrieve files from the users OneDrive's account, we set up an AAD application in order to allow the access to the required resources.

After configuring it on Azure portal, we wrote the next code snippet:

    /// <summary>
    /// The OneDrive business client factory.
    /// </summary>
    internal sealed class OneDriveBusinessClientFactory : IOneDriveClientFactory
    {
        /// <summary>
        /// Creates an instance of the OneDrive client.
        /// </summary>
        /// <param name="data">The authentication data required to build the OneDrive client.</param>
        /// <returns>An instance of <see cref="IOneDriveClient"/>.</returns>
        public async Task<IOneDriveClient> CreateAsync(IAuthenticationData data)
        {
            var adalAuthenticationProvider = new AdalAuthenticationProvider(data.ClientId, data.Secret, data.ReturnUrl);
            var discoveryServiceHelper = new DiscoveryServiceHelper(adalAuthenticationProvider);
            var businessServiceInformation = await discoveryServiceHelper.DiscoverFilesEndpointInformationForUserAsync();
            await adalAuthenticationProvider.AuthenticateUserAsync(businessServiceInformation.ServiceResourceId);

            return new OneDriveClient(data.BaseUrl, adalAuthenticationProvider);
        }
    }

The situation is:

Microsoft.IdentityModel.Clients.ActiveDirectory Error: 4 : 9/23/2016 12:19:16 PM: xxxxxxx - <RunAsync>d__0: Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently. Call method AcquireToken
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenSilentHandler.SendTokenRequestAsync()
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<RunAsync>d__0.MoveNext()
    ErrorCode: failed_to_acquire_token_silently

My questions are:

Thanks in advance.

Regards, Ángel

cdmayer commented 7 years ago

The silent token request exception is expected (when the code can't be renewed), but it should also be handled automatically. Are you saying it rises all the way to your client code?

For the second one, you're saying that you get itemNotFound when you try to request the root of the drive? Can you post the code you are using for that, and the exception details?

Also, can you post your ServiceResourceId? I want to make sure that URL is formatted correctly.

huorswords commented 7 years ago

Thank you by your answer.

Yes, my code is catching an exception every time that I'm trying to connect using

    public AdalAuthenticationProvider(string clientId, string clientSecret, string returnUrl, AuthenticationContext authenticationContext = null);

but not when I use the

    public AdalAuthenticationProvider(string clientId, string returnUrl, AuthenticationContext authenticationContext = null);

override.

At the other hand, the itemNotFound error code appears when I try to recover the drive's root. The code that I'm using is:

    var request = this.oneDriveClient.Drive.Items[parent].Children.Request().GetAsync();
    var collectionPage = await request;

This code works perfectly when I execute it connected to an OneDrive personal account client.

Our ServiceResourceId is https://pasiona1-my.sharepoint.com/.

cdmayer commented 7 years ago

I am investigating the authentication issue.

What is the parent variable in the context you provided? Why don't you use this.oneDriveClient.Drive.Root.Children.Request().GetAsync() to get the children of the root?

huorswords commented 7 years ago

The parent value is, depending on the recursive call tree, the id of the folder element that I'm processing. At first time, it is the root element id value.

The owner method of the code shown is used to search and retrieve recursively all media files from a given directory id.

cdmayer commented 7 years ago

I'm working on getting a repro of this issue. So far I'm unsuccessful. Is your app a UWP app or a desktop/native app? That impacts which flavor of AuthenticationProvider is created.

huorswords commented 7 years ago

Thank you by your efforts, Chris.

Right now, I'm experiencing this issue with a Windows Forms application.

cdmayer commented 7 years ago

Hello, Really sorry about the delay.

It sounds like your issue is related to the configuration of your app. Please visit this page and make sure you have configured it correctly.

Additionally, since this is as desktop app you should not be providing a client secret (as there is no way for your client to safely pass that info to the login service). Make sure to use the constructor that does not use a client secret.

Once you have done those steps please report back. I'll also try to help you more quickly than last time :)

huorswords commented 7 years ago

I'll try to figure out what is wrong with the configuration and I'll be back to explain the progress.

Thanks!

huorswords commented 7 years ago

Excuse me by the delay.

I couldn't test the problem properly from the last time I posted, so I'm closing the issue and will reopen it when I have any conclusion about the problem.

Sorry for inconvenience & thanks for your support.