Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneDrive SDK Authentication Issue in Console App

Tags:

c#

onedrive

I am coding against OneDrive SDK within a console application. I am having some trouble trying to authenticate properly. I am curious to see if anyone has done this before or could point me in the right direction?

Code so far:

 [STAThread]
    static void Main(string[] args)
    {
        var scopes = new[] {"onedrive.readonly", "wl.signin"};
        var msaAuthProvider = new MsaAuthenticationProvider(ClientId, "https://login.live.com/oauth20_desktop.srf", scopes);

        msaAuthProvider.AuthenticateUserAsync();
    }

Whenever I run my debugger I see the property of isAuthenticated is set to false.

like image 355
Jay Dave Avatar asked Sep 11 '25 12:09

Jay Dave


1 Answers

Right now the MSA Authentication only supports Desktop and Windows Mobile Apps as it needs UI to require user sign in which broke in your case because you have a console app. So the problem is to build a IAuthenticationProvider which set the Authorization header in request message like:

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        request.Headers.Authorization = new AuthenticationHeaderValue("bearer", await GetAccessTokenFromSomeWhere());
    }

The IAuthenticationProvider interface defined in Microsoft.Graph.Core and you can pass it into OneDriveClient constructor so OneDriveClient will make request to OneDrive API for you.

Based on the information you have a console app, I would suggest you look at the code flow for Microsoft OAuth to get an access token and pass it to your authentication proivder.

I am not sure how you can get a code from the OAuth flow, but it seems doable if you fetch it in somewhere and pass it to your console app, then you can make POST request the the code you have to redeem an access token. I have searched for a while and don't find a good example of console app to get OAuth for Microsoft account, sorry. If you find a good way for console app to authentication, feel free to send a PR for MSAAuthenticator.

like image 89
dabox Avatar answered Sep 14 '25 03:09

dabox