x

Test code for POST oauth2/token endpoint

Hello, 

I'm testing my code and looks like can't get it right how to obtain the code for obtain_token endpoint. What i do now is: I add an error raise just before oauth2/token call in my code (together with outputting the code into the log), then i go into my app in the browser (in dev mode), i initiate connect with my square seller account (in the browser manually), then it fails (because i added raise). Then i go to the log, i take the code (which was not used, so it's still valid). Then i use it in my test. That takes a lot of time

Is there a better way to do that? 🙂 Am i missing something? Is there a test code for oauth2/token endpoint? 

1,580 Views
Message 1 of 6
Report
1 Best Answer
Square Community Moderator

Best Answer

Hey! I'm stepping in for the other Ashley real fast. We do not offer a way of automating the authorize url; someone/something must navigate to it and click “Allow”. There’s tools for browser testing, such as Selenium, but know that that is outside of Square's support and all help will need to go through that company. However, the rest of it (retrieving the code, using the code) can be done by the OAuth example provided above.

AshleyK
Community Moderator, Square
Sign in and click Mark as Best Answer if my reply answers your question.

View Best Answer >

1,442 Views
Message 6 of 6
Report
5 REPLIES 5
Square Community Moderator

Hello again @Mikhail,

 

I reached out to the API team and they replied with the following:

 

"There’s no test codes for sandbox, they’ll need to create their own (which it sounds like they’re doing in an odd way?). After a merchant clicks on the allow on the authorize link, Square should redirect them to their OAuth page.

 

In the code, they should simply be able to retrieve the code via the POST parameters like any other page. Not sure how to show an example of that without knowing what language they’re using, but there’s a full walkthrough here in

PHP: https://developer.squareup.com/docs/oauth-api/walkthrough "

Ashley C
Community Moderator, Square
Sign in and click Mark as Best Answer if my reply answers your question.
1,490 Views
Message 2 of 6
Report

Hello Ashley, 

could you please tell me how to retrieve the code in ruby?


In the code, they should simply be able to retrieve the code via the POST parameters like any other page.

1,484 Views
Message 3 of 6
Report
Square Community Moderator

@Mikhail,

 

Apologies for the delayed responses! I'm the middle man with the API team so I was awaiting their response haha.

 

They said they believe you can just do:

 

 

and they also provided a fully working OAuth ruby example here.

Ashley C
Community Moderator, Square
Sign in and click Mark as Best Answer if my reply answers your question.
1,466 Views
Message 4 of 6
Report

Hi Ashley,

 

i don't think we are on the same page. It seems to me you are showing me how to use the square API in production/development to connect an account. I already have this set up. Everything is working nicely and there is no problem with it. Like i explained you in my initial message: i need the code for my automated tests. These tests are testing only the backend functionality (my backend services). So there is no _params_ or anything like this, there is no browser involved. 

In order to write my automated tests i need to have the code (which in development/production comes from the front-end, but now it's not an option). So how should i test it?

My backend service calls auth2/token endpoint. I want to test this call and everything that happens around it. Like i said in my initial message what i do at the moment is i get this code _manually by running my app in the browser_ and getting this code (but not using it), saving it and then using it in my automated test (_once_). Which is not how automated testing should be done i believe.

So what do you suggest in order to write automated backend tests for the service that calls your outh2/token endpoint (and needs the code)? Is there a way to get the code from the backend (in order to pass it to this service)? If there is no way to do that how would you suggest testing backend service that calls this endpoint?

1,459 Views
Message 5 of 6
Report
Square Community Moderator

Best Answer

Hey! I'm stepping in for the other Ashley real fast. We do not offer a way of automating the authorize url; someone/something must navigate to it and click “Allow”. There’s tools for browser testing, such as Selenium, but know that that is outside of Square's support and all help will need to go through that company. However, the rest of it (retrieving the code, using the code) can be done by the OAuth example provided above.

AshleyK
Community Moderator, Square
Sign in and click Mark as Best Answer if my reply answers your question.
1,443 Views
Message 6 of 6
Report