Skip to content
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

Getting "expired authorization code" error while creating JWT token #31

Closed
Ashutosh2547 opened this issue May 23, 2024 · 8 comments · Fixed by #39 or #41
Closed

Getting "expired authorization code" error while creating JWT token #31

Ashutosh2547 opened this issue May 23, 2024 · 8 comments · Fixed by #39 or #41
Labels
bug Something isn't working

Comments

@Ashutosh2547
Copy link

Ashutosh2547 commented May 23, 2024

Description

After configuring the config.yml to point to the connected app and using the grant_type "jwt-bearer". Getting 400 bad request

Steps to Reproduce

  1. Clone the repository
  2. Create a connected app in SDFC with the configurations provided in readme - Link
  3. Update the config.yml file with the new relic license key and account id
  4. Run the app

Getting below error in auth.py

raise LoginException(f'sfdc token request failed. http-status-code:{resp.status_code}, reason: {resp.text}, full response {resp}')
newrelic_logging.LoginException: sfdc token request failed. http-status-code:400, reason: {"error":"invalid_grant","error_description":"expired authorization code"}, full response <Response [400]>

Expected Behavior

JWT token should be retrieved and app should pull the data from SFDC.

Relevant Logs / Console output

File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src_main_.py", line 272, in main
run(config, event_type_fields_mapping, numeric_fields_list)
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src_main_.py", line 213, in run
run_once(
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src_main_.py", line 165, in run_once
).run()
^^^^^
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\integration.py", line 41, in run
raise e
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\integration.py", line 37, in run
instance.harvest(session)
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\instance.py", line 24, in harvest
self.api.authenticate(session)
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\api.py", line 81, in authenticate
self.authenticator.authenticate(session)
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\auth.py", line 197, in authenticate
self.authenticate_with_jwt(session)
File "C:\Application_Development\Personal\newrelic-salesforce-exporter\src\newrelic_logging\auth.py", line 147, in authenticate_with_jwt
raise LoginException(f'sfdc token request failed. http-status-code:{resp.status_code}, reason: {resp.text}, full response {resp}')
newrelic_logging.LoginException: sfdc token request failed. http-status-code:400, reason: {"error":"invalid_grant","error_description":"expired authorization code"}, full response <Response [400]>
2024-05-23 19:10:42,525 (15736/MainThread) newrelic.core.agent INFO - New Relic Python Agent Shutdown
NOTE: # ( Please provide specifics of the local error logs, Browser Dev Tools console, etc. if appropriate and possible. )

Your Environment

  • ex: Browser name and version:
  • ex: Operating System and version:

Additional context

@Ashutosh2547 Ashutosh2547 added the bug Something isn't working label May 23, 2024
@kanwaljit-mq
Copy link

Subscribing, getting same issue

@sdewitt-newrelic
Copy link
Contributor

sdewitt-newrelic commented May 29, 2024

Hi @Ashutosh2547 and/or @kanwaljit-mq can you please provide your full config.yml? Before pasting, please make sure to remove any of the sensitive information such as keys, usernames, passwords, etc. I do not need those, just the rest of the configuration.

I have found two things I would like to ask you to check on the Salesforce side.

Can you ensure what the expiration date on the token is set to? I think this may be the "Token Timeout" described in the last paragraph here.

Can you also check the system time on the machine where the exporter is running? I've seen a few posts where people indicated that incorrect system times caused this error.

@kanwaljit-mq
Copy link

@Ashutosh2547 This is how I fixed the issue and I am sure there is an elegant way to do it besides this.
In file :/src/newrelic_logging/auth.py [Ref]
Go to Line #107 exp = int((datetime.utcnow() - timedelta(minutes=5)).timestamp())
P.S. Line number may be different based on branch you are on.
Update the minus to plus. This is where the session expiry time is set for your (Bearer) token. You can increase the number (5) it to however more/less needed for your use case.
So , it should look like : exp = int((datetime.utcnow() + timedelta(minutes=5)).timestamp())
HTH

@Ashutosh2547
Copy link
Author

Thanks @kanwaljit-mq ,
This fix worked for me.

@sdewitt-newrelic
Copy link
Contributor

Hi @kanwaljit-mq and @Ashutosh2547 , can you update your remote and checkout the branch fix/jwt-expired-auth-code and see if this fixes your issue? The fix is the same as what you suggested (add instead of subtract) but also exposes a configuration variable for this value (default is 5).

@sdewitt-newrelic
Copy link
Contributor

@kanwaljit-mq @Ashutosh2547 PR #39 is now merged to main. Please update your remote and give this a try.

@sdewitt-newrelic
Copy link
Contributor

Reopening this issue to address the error reported by @kanwaljit-mq here

@sdewitt-newrelic
Copy link
Contributor

@kanwaljit-mq This should fix it. I admit that I don't have an environment to test JWT authentication so it's possible there are still issues. Please give this a go and let me know how it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants