k-capehart / go-salesforce

Salesforce REST API client written in Go
https://pkg.go.dev/github.com/k-capehart/go-salesforce/v2
MIT License
30 stars 6 forks source link

Access token is expiring for client credentials flow, Implement login in Init() to handle refresh of access token #56

Closed aishmuru closed 3 months ago

aishmuru commented 3 months ago

@k-capehart,

We are using client credentials flow to generate the token at the starting of Golang server. Can you please suggest how to use this package to have the non-expiring token / the token that refreshes before the older one expires. Is there any middleware that handles token expiry. If the solution does not exist already, can we add a new feature to handle the refresh token.

We are using Init() method at starting of server.

Thanks! Aishwarya

jozephp commented 3 months ago

To summarize you are looking to regenerate the access token and update the sfClient object with the new access token

k-capehart commented 3 months ago

Hey @aishmuru , thanks for bringing this up. It's a feature that I've wanted to implement but hadn't decided how yet. But I'll definitely look into it now 😄

I'd love to be able to use a refresh token, but unfortunately it is only generated from the web server, user-agent, or hybrid app token flow.

One option that I think would work for now would be to simply call the Init() method again, passing the same credentials, and set the value to your existing sfClient instance.

Here's some thoughts on a new solution.

I like the idea of doing it automatically, but I'll have to verify that I can reliably catch the right error. What error message/status code did you get for an expired session?

aishmuru commented 3 months ago

@k-capehart Thanks for your response.

Can you please take this feature enhancement on priority? This is problem for all, who are using client credentials flow. The token will expire so the existing package is only be useful for few hours after starting server.

k-capehart commented 3 months ago

@aishmuru Did you try the temporary solution I mentioned above? If you get an INVALID_SESSION error you can call Init() again to manually refresh your session.

You can also extend your timeout value to 24 hours in your Session Settings in your Salesforce org.

I'll let you know when the full solution is rolled out.

k-capehart commented 3 months ago

See above for the PR. It's mostly done, just needs some extra tests. Feel free to check out the branch and point your project to it if you need it ASAP. I won't have a chance again until Monday or Tuesday. But again, you should be able do a work-around like I mentioned above.

k-capehart commented 3 months ago

@aishmuru This has been implemented as part of v2.1.0. Let me know if you run into any more issues! To upgrade: go get github.com/k-capehart/go-salesforce/v2@v2.1.0

jozephp commented 3 months ago

thanks @k-capehart will check

jozephp commented 3 months ago

@k-capehart I observed that DoRequest() generates a requestPayload{} without retry flag.

https://github.com/k-capehart/go-salesforce/blob/5096c4d0aeb41f9de11135c55e04559b1664413d/salesforce.go#L244C34-L244C48

How should we set this flag to ensure a rety logic is triggered on token expiration?

Thanks in advance!

k-capehart commented 3 months ago

Hey @jozephp , anytime a request is made and returns the INVALID_SESSION_ID error, it will set the retry flag to true and attempt again. It is always submitted initially as false. The flag is just to ensure you don't get caught in an endless loop.

I just tested it with the DoRequest() method and it works as expected. That is, it will refresh the session and attempt to retry. If you're still noticing an issue or have a specific example where it's failing then please open a new Issue so I can look into it further.

image