Reputation: 1617
I have made a service that reads new emails from my email and does operations on it, it should be running as a windows service but the problem is that the very first time you run the app it prompts a google access authentication, only once. I can run it as console app once, authenticate and then run it unlimited number of times without the need to authenticate, but when running the project as a windows service the app won't work because there is no prompt window to authorize the access, anyone has an idea or a source that could help me?
EDIT***
I can see that after the first auth it creates the token.json & then the app uses it to authenticate without the prompt, why cant the service read it?
this is how it looks :
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath ,false )).Result;
}
FileDataStore created token.json folder + file on first run, then reused it, so i don't know why the app running as a windows service couldn't read it from there like it did on the console app.
Upvotes: 0
Views: 406
Reputation: 85
Windows service reads the app.config file when it starts. Therefore if you changed some value in app.config later you either need to restart the service or you can use this code to refresh the section which contains your token and then read it.
See: original answer from jdigaetano
ConfigurationManager.RefreshSection("appSettings")
sValue = ConfigurationManager.AppSettings(name)
You can find out more about RefreshSection in documentation.
You can create new key in appSettings
<appSettings>
<add key="path" value="" />
</appSettings>
Then if you want to save path to your token.json the config file, you can do that like this
Configuration config = ConfigurationManager
.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["path"].Value = "pathToTokenJson";
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
Please note that you need to add reference to System.Configuration
Upvotes: 1