2

I'm writing a windows service application, the app can run in background monthes without user interaction, I want that this app will upload files to OneDrive using Microsoft.Graph api. I know how to sign in from code and also succeded to upload files.

I started with simple console app, when I sign in there is some Windows's window that askes user name and password, and after that I'm filling the user name and password I'm getting some token from the server response. the token experis after like an hour.

how can I make the token be relevat for long time, like monthes, my goal is that I don't want to sign in in the windows's window every time.

Overview of Microsoft Graph

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Codey
  • 487
  • 1
  • 8
  • 27

1 Answers1

2

You cannot control the lifetime of a token. That is controlled by the Tenant Administrator.

For daemons/services, what you most likely want is an "App-Only" token. This uses a slightly different OAuth grant called Client Credentials and, unlike the Authorization Code grant you're using today, it does not require a user to sign in. You can read more about this in the documentation under Get access without a user.

If you want to remain using an "App+User" token (i.e. Authorization Code grant), then you need to get a Refresh Token by requesting the offline_access scope. You'll need to store this refresh token on the local machine (I strongly suggest you protect it like you would a password). You can then exchange that refresh token for a fresh access token when needed. You can find details on refreshing tokens in the documentation under Use the refresh token to get a new access token.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63