Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Staying authenticated with the Onedrive SDK/API (Or any API)

I'm tinkering with the OneDrive API.

Find the code here https://github.com/onedrive/onedrive-sdk-csharp

Specificaly the OneDrive Api Browser.

I have no formal education on this specific subject (Authentication).

I want to know, how would I stay authenticated after the first login? That is, how would I store the login information when it seems that you are expected to query the URI for a token every time?

For example, when the OneDrive API Browser solution is run, You must sign in every time the app is run. What if I wanted to save the credentials somewhere, say in a text file? How would I do that? (I am aware of the security issues / poor practice there)

Should I save the token somewhere? Is there another service to use for longterm tokens? Is it even possible? Are cookies involved?

like image 895
Frostytheswimmer Avatar asked Mar 03 '16 12:03

Frostytheswimmer


People also ask

Is there an API for OneDrive?

The OneDrive REST API is a portion of the Microsoft Graph API which allows your app to connect to content stored in OneDrive and SharePoint.


1 Answers

Conceptually after you authenticated your app using code flow, you will receive a refresh token which you can save along with your client Id and use it later to retrieve the access token as described here . For windows and windows phone you can also use the Authentication Adapter for the OneDrive SDK which does all the job:

MsaAuthenticationProvider msaAuthProvider = new MsaAuthenticationProvider(
                            MsaClientId,
                            MsaClientSecret,
                            RedirectUri,
                            Scopes,
                            null,
                            new CredentialVault(MsaClientId));

        await msaAuthProvider.RestoreMostRecentFromCacheOrAuthenticateUserAsync(userName);
        OneDriveClient oneDriveClient = new OneDriveClient("https://api.onedrive.com/v1.0", msaAuthProvider);
like image 82
Behzad Shams Avatar answered Sep 27 '22 17:09

Behzad Shams