Sage Sage Posted December 8, 2022 Share Posted December 8, 2022 Hello, I'm a student and I develop a Java Web Application that supposed to get and post data beetween Web App and Sage Accounting. The post request (https://oauth.accounting.sage.com/token) to get token always returns 403. I put here my code. Thanks! Link to comment Share on other sites More sharing options...
Steel, Mark Posted December 8, 2022 Share Posted December 8, 2022 Hi, thank you for your question. Could you confirm that you are successfully obtaining the code returned to your callback URL and then make the request to the token endpoint before the code expires? In the example you provide the code looks to be static? Could you share the method you're using for the authorization request as per https://developer.sage.com/accounting/guides/authenticating/authentication/ please. Thanks Mark Link to comment Share on other sites More sharing options...
Sage Sage Posted December 9, 2022 Author Share Posted December 9, 2022 Hello! Thank you for replying me. When I just take the code in URL an try it on postman, it works, it sends a token (knowing that the code expires after 60 seconds and we can't use it twice) Link to comment Share on other sites More sharing options...
Steel, Mark Posted December 9, 2022 Share Posted December 9, 2022 What does the request look like in the Postman console? Below is an example nodejs method used to exchange the code for tokens. You can see it uses the qs parsing library to stringify the request parameters before passing the request via axios. If you're confident the code has not expired by the time you're making the request in your method try parsing the body parameters in a similar way to the below. Thanks Mark const exchange_code = async(code)=>{ //The qs library is required to stringify the data for url encoding var data = qs.stringify({ client_id: String(process.env.client_id), client_secret: String(process.env.client_secret), code: code, grant_type: 'authorization_code', redirect_uri: String(config.callback_url), }); var cfg = { method: 'post', url: String(config.access_token_url), headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json' }, data : data } try { //Pass the config to axios and make the post request const resdata = await axios(cfg) var credentials = resdata.data; Link to comment Share on other sites More sharing options...
Sage Sage Posted December 12, 2022 Author Share Posted December 12, 2022 (edited) Thanks for the code example but now I have error 400 "DataParsingError The data you sent could not be processed" A kind of syntax error with request body Edited December 12, 2022 by Sage Sage Link to comment Share on other sites More sharing options...
Steel, Mark Posted December 13, 2022 Share Posted December 13, 2022 Thanks for the update. Can you confirm, what callback url's do you have registered in Developer Self Service? Is the callback url "http://localhost:8084/getionparcGlpi/connexion" registered there? and also, how you are obtaining the code to exchange in the request? Thanks Mark Link to comment Share on other sites More sharing options...
Sage Sage Posted December 13, 2022 Author Share Posted December 13, 2022 Hello! Yes, I can confirm that my callback url is correct. I've made some updates to my java code. As you can see, I'm redirected to the callback URL, with an authorization code. I'm blocked with the step "Exchange the authorization code for the access token". I get error 400. (It's the same error on postman if we dont send the body parameters as x-www-form-urlencoder, so I think the problem comes to the syntax of the body parameters) Link to comment Share on other sites More sharing options...
Steel, Mark Posted December 13, 2022 Share Posted December 13, 2022 You'll need to resolve the data parsing error and revert to your original code and pass the body params as they were. Link to comment Share on other sites More sharing options...
Sage Sage Posted December 15, 2022 Author Share Posted December 15, 2022 Hello. I find a solution to get token from Java API. There is my code. It uses some apache libraries: (HttpClient and HttpCore) Link to comment Share on other sites More sharing options...
Steel, Mark Posted December 15, 2022 Share Posted December 15, 2022 Thanks for sharing your solution and good luck with your project. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now