New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authenticating / Refreshing Google API access token and avoiding a “file in use” exception? #1708
Comments
@amanda-tarafa i forwarded them over here. I have never seen this issue before with the library. In my experience the file lock is always released after it reads it so i'm not sure why the lock would remain. However my knowledge of this library in VB.net is very limited |
I suspect the problem may be caused by calling I would have hoped that the library would be thread-safe and not try to perform multiple operations on the file anyway, and we should look into that, but I'm also hoping that the code given can be simplified to remove the manual refresh. |
Hi @jskeet. Thanks for your messages. I have simply coded it as best as I understood it. Sounds like you are proposing a better, more simpler way for me to handle authentication all together? Can I kindly ask for an example based around what I have at the moment? I had to add the refresh approach in because users weren't using my calendar sync too regularly and the token was expiring. I think the majority of what I have show you I extracted from examples online. |
But if you're using |
In my
Then, after setting up some logging I do this:
Where XXX is the name of my tool. Was not sure if it was safe to expose the name of the tool on here? My app is a console app and has some command line switches. So if I pass
But in our particular case, we had already authenticated and already had a data file in the user folder. And it was running my
What I don't understand is that you are telling it it is all supposed to work under the hood, and I admit that my Outlook Calendar tool which does the same stuff is much simpler and I don't have to worry about anything at all. But I always had problems with Google API. Going back to basics then. My master application shows a window and wants to display a list of calendars from the user for them to choose one to sync with. They will be in one of two scenarios:
It sounds like you are tell me that I don't need to worry about any of it? |
I would suggest avoiding writing out your own authentication file. You're already using an auth file here: That authentication flow will (IIRC) detect if the file exists and use the existing authentication if it's present and valid for the scopes you're requesting, or prompt the user if necessary. There's no need to do anything more, as far as I can see. |
Your suspicion is right, definetely You'll have to turn your method into an async method, like such:
I would also recommend to await the call to
Also, I see that you are returning the tokens as part of your method, so I suppose you are using them somewhere else and need them fresh? If I may ask, what are you using them for, since you've already instantiated the CalendarService using the credential? If you don't need them for anything else, you don't need to refresh the token explicitly here, when the calendar service requests a token from the credential, the credential will check whether the token has expired or is close to expiring and will refresh it and always give you a fresh one. |
I just tried changing my code to use I need to populate the passed in
So that other calls can use it. Would it be better here to change
In short, I don't really remember why I did that. I started writing this tool along time ago and I most likely (obviously) didn't understand the mechanics of it. I am just getting a bit confused .... |
Yes, But, as @jskeet suggested, I would advise you stop returning and writing in a file the tokens yourself, and also refreshing the tokens. Your authenticate method will turn into something like this:
And then, as long as you are using You can see a C# example here in the method called |
Thanks @amanda-tarafa ! I am taking this one step at a time. I have changed my function. As a result, I have two further issues (for now).
2, We no longer have a
UpdateI assume that point 2 is redundant. It turns out that I don't actually use |
It seems that if I simply change this bit to:
Then it will be Ok to use the And it is Ok to comment out that |
You should generally not use the Instead, you should make this calling method async as well, and await the task. |
@jskeet I am calling this from |
It's always better to do Await, calling Result will block the calling thread and could in some cases lead to deadlocks. Here is some starter docs on Asynchronous programming in .NET
You said yourself you are not using those, right? The following line is only creating an instance of the
|
@amanda-tarafa Thanks for the clarifications. I think the And thanks for the link on async programming. I have now changed my
But it won't compile now:
|
So I have now moved all of my main "main" code into a function:
And now my
I still have to move all the obsolete calls to logging of token info to the INI, but apart from that is the above correct? Thanks. |
I realise Amanda said:
But eventually we end up in |
Yes, given that yours is a console application, it should be fine as it is now. Just calling Wait() and Result is redundant. You can just do:
|
Thank you! Fantastic. :) I will give all this ago. I may end up asking a related question about obtaining the calendar list. But I won't bloat this discussion. I will give it all a try and see if my exceptions stop now. |
Whilst I am in the middle of deleting all references to maintaining my own copies of tokens, I now see why. I don't know if this should be split into a new question. In my main application it has a option to remove Google access. At the moment it is doing this:
The above is a Visual C++ MFC project and as you can see, it needed the token. Now, I also have another function:
This one is called at the bottom of the previous one. The
If I am to remove any requirement for my main application to need knowledge of tokens I need to either migrate this code into VB.Net or find another solution. I am open to guidance as I am now in the middle of changes to a main app that no longer compiles. Thanks! |
You don't need to do any of that, there's a Revoke method on the UserCredential. You can do something like the following (I'm writing directly on the comment so there might be some syntax errors).
This will both revoke the token with the Google Auth platform and delete it from the local file. |
Thanks. Since I call Or, if I omit the |
Just tried the latter and it functions OK. Great! I will continue testing with a test calendar before I close this discussion. |
It seems to work great. You have been very helpful. I'll ask the other question shortly. Thanks again! |
I have been encouraged to ask my question that I raised on StackOverflow here.
In short, I have been having occasional issues when trying to sync with a Google Calendar.
The basic functionality is:
The delete / add tasks are done using batching.
As mentioned, sometimes I get an exception like:
This is how I authenticate:
I was wondering if this line is wrong:
credential.RefreshTokenAsync(CancellationToken.None)
Should it be proceeded with
await
? I wondered if it might still be refreshing the token file whilst we are continuing to try and use it. I can provide further code snippets as needed.As mentioned, it usually works and this is a sporadic issue.
The text was updated successfully, but these errors were encountered: