Forums

error in task but not in running the code

Hi I have this error in my task, but not at all when I run the code. Can you help me and explain to me why and how to manage ?

I repeat the token is ok and my code is working when I run it.

Kind regards Antoine

2023-08-08 21:25:36 - Task preparing to start
Aug  8 21:25:54 Traceback (most recent call last):
Aug  8 21:25:54   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 39, in authenticateGmailAPIs
Aug  8 21:25:54     creds.refresh(Request())
Aug  8 21:25:54   File "/usr/local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 302, in refresh
Aug  8 21:25:54     ) = reauth.refresh_grant(
Aug  8 21:25:54   File "/usr/local/lib/python3.10/site-packages/google/oauth2/reauth.py", line 347, in refresh_grant
Aug  8 21:25:54     _client._handle_error_response(response_data)
Aug  8 21:25:54   File "/usr/local/lib/python3.10/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response
Aug  8 21:25:54     raise exceptions.RefreshError(error_details, response_data)
Aug  8 21:25:54 google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})
Aug  8 21:25:54 During handling of the above exception, another exception occurred:
Aug  8 21:25:54 Traceback (most recent call last):
Aug  8 21:25:54   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 540, in <module>
Aug  8 21:25:54     main_extended()
Aug  8 21:25:54   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 505, in main_extended
Aug  8 21:25:54     service = authenticateGmailAPIs()
Aug  8 21:25:54   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 46, in authenticateGmailAPIs
Aug  8 21:25:54     send_warning_email(f"Error occurred while refreshing or authenticating the Gmail API token: {str(e)}")
Aug  8 21:25:54   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 53, in send_warning_email
Aug  8 21:25:54     service = build('gmail', 'v1', credentials=creds)
Aug  8 21:25:54 NameError: name 'creds' is not defined. Did you mean: 'credits'?
Aug  8 21:25:58 Traceback (most recent call last):
Aug  8 21:25:58   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 39, in authenticateGmailAPIs
Aug  8 21:25:58     creds.refresh(Request())
Aug  8 21:25:58   File "/usr/local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 302, in refresh
Aug  8 21:25:58     ) = reauth.refresh_grant(
Aug  8 21:25:58   File "/usr/local/lib/python3.10/site-packages/google/oauth2/reauth.py", line 347, in refresh_grant
Aug  8 21:25:58     _client._handle_error_response(response_data)
Aug  8 21:25:58   File "/usr/local/lib/python3.10/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response
Aug  8 21:25:58     raise exceptions.RefreshError(error_details, response_data)
Aug  8 21:25:58 google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})
Aug  8 21:25:58 During handling of the above exception, another exception occurred:
Aug  8 21:25:58 Traceback (most recent call last):
Aug  8 21:25:58   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 540, in <module>
Aug  8 21:25:58     main_extended()
Aug  8 21:25:58   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 505, in main_extended
Aug  8 21:25:58     service = authenticateGmailAPIs()
Aug  8 21:25:58   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 46, in authenticateGmailAPIs
Aug  8 21:25:58     send_warning_email(f"Error occurred while refreshing or authenticating the Gmail API token: {str(e)}")
Aug  8 21:25:58   File "/home/Digimidi/gmail/gmailwhispergptandback.py", line 53, in send_warning_email
Aug  8 21:25:58     service = build('gmail', 'v1', credentials=creds)
Aug  8 21:25:58 NameError: name 'creds' is not defined. Did you mean: 'credits'?

How the creds objects is made accessible to the code?

Sorry I do not understand ? A task should be a different code than the python script itself ?

NameError: name 'creds' is not defined. in the log (at the bottom) suggests that your code is trying to use an object creds which is not available, hence my question what are the assumptions about that object's availability.

I see but the python script works and don't recall any error. this is why I'm lost

Can you show the full command you use in the task and name the Python script that is working separately?

÷ Command § Description ÷ State Actions python3.10 /home/Digimidi/gmail/gmailwhispergptandback.py

/home/Digimidi/gmail/gmailwhispergptandback.py

thank you for the help

I really don't understand.

I've remove the error handling form my script and still get the error message. But the token is OK because I can run the script properly...

2023-08-09 11:25:33 - Task preparing to start Aug 9 11:25:52 Traceback (most recent call last): Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 675, in <module> Aug 9 11:25:52 main_extended() Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 633, in main_extended Aug 9 11:25:52 service = authenticateGmailAPIs() Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 39, in authenticateGmailAPIs Aug 9 11:25:52 creds.refresh(Request()) Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 302, in refresh Aug 9 11:25:52 ) = reauth.refresh_grant( Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/reauth.py", line 347, in refresh_grant Aug 9 11:25:52 _client._handle_error_response(response_data) Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response Aug 9 11:25:52 raise exceptions.RefreshError(error_details, response_data) Aug 9 11:25:52 google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

I really don't understand.

I've remove the error handling form my script and still get the error message. But the token is OK because I can run the script properly...

2023-08-09 11:25:33 - Task preparing to start Aug 9 11:25:52 Traceback (most recent call last): Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 675, in <module> Aug 9 11:25:52 main_extended() Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 633, in main_extended Aug 9 11:25:52 service = authenticateGmailAPIs() Aug 9 11:25:52 File "/home/Digimidi/gmail/gmail_audiowhisper_emailtexttogpt.py", line 39, in authenticateGmailAPIs Aug 9 11:25:52 creds.refresh(Request()) Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/credentials.py", line 302, in refresh Aug 9 11:25:52 ) = reauth.refresh_grant( Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/reauth.py", line 347, in refresh_grant Aug 9 11:25:52 _client._handle_error_response(response_data) Aug 9 11:25:52 File "/usr/local/lib/python3.10/site-packages/google/oauth2/_client.py", line 60, in _handle_error_response Aug 9 11:25:52 raise exceptions.RefreshError(error_details, response_data) Aug 9 11:25:52 google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

for me it looks like the task is looking into another directory which is not the current directory for the token because when I remove a token from another directory I have another error

2023-08-09 11:30:39 - Task preparing to start Aug 9 11:30:54 #033[?1049h#033[22;0;0t#033[1;24r#033(B#033[m#033[4l#033[?7h#033[?1h#033=#033[39;49m#033[39;49m#033[37m#033[40m#033[H#033[2J#033[37m#033[40m#033[J#033(B#033[m#033[39;49m#033[37m#033[40m#033[39;49m#033[37m#033[40m#033[J#033[39;49m#033(B#033[m#033[22d#033[93m#033[44mGetting https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=#033[K#033[39;49m#033(B#033[m#015#033[93m#033[44mLooking up accounts.google.com#033[K#033[39;49m#033(B#033[m#015#033[93m#033[44mMaking HTTPS connection to accounts.google.com#033[39;49m#033(B#033[m#015

But the script exactly set the directory to look for the token so where is the problem ?

Thank you

please can you help me ?

If you're getting different behaviour when running a script in a console to what you see in an always-on task, the problem is likely to be the working directory. If, in a console, you're doing something like:

cd somedirectory
python somescript.py

...whereas in an always-on task you're doing something like:

python somedirectory/somescript.py

...then you will have a different working directory for the always-on task, so references to files using relative paths -- for example "config.txt" -- will reference different filesystem locations.

In general, it's best to do the same in the always-on task as you do in the console, and cd to the directory before running the script. You can separate multiple commands with semicolons, so the task config would look like this for the example above:

cd somedirectory; python somescript.py