1
We are developing a call to an SMS sending Web Service that uses Oauth2 authentication. This development is being carried out in groovy. We searched in several forums how to elaborate the call, passing the parameters and how to receive the response of the web service. We are doubtful if we are developing in the right way, since we do not have clear information about the code’s Steps. Attached we have what the service can answer, below the result of a call via Linux.
$ curl URL_PARA_PEGAR_O_TOKEN -d
"grant_type=client_credentials&client_id=USUARIO&client_secret=SENHA"
Retorno:
{"token_type":"bearer","access_token":"TOKEN_RETORNADO","expires_in":7200}
$ curl URL_DO_SERVICO \
-H "Content-Type: application/json" \
-H "Authorization: Bearer TOKEN_RETORNADO" \
-X POST \
-d 'JSON A SER ENVIADO'
They could help us structure the code, criticizing possible errors or suggesting some insertion of some missing point?
@Grapes([
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6-SNAPSHOT' )
])
import groovyx.net.http.RESTClient
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.URLENC
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.POST
//def client = new RESTClient ( 'http://localhost:8080' )
//def site = new HTTPBuilder( 'http://localhost:8080' )
//site.auth.basic 'my-client-with-secret-2', 'secret-2'
//site.auth.basic 'dmac', 'foo'
//site.auth.basic 'MC101', 'MC101'
def credentials = [
client_id:"<client-id>",
client_secret:"<client-secret>",
site:"https://ms.foo.com.br
]
def accessToken
site.post(path: '/oauth2/token',
body: [grant_type:'client_credentials',client_id='client_id',client_secret='client-secret'],
requestContentType: URLENC ) { resp, reader ->
accessToken = reader['access_token']
}
//println("Access Token: ${accessToken}")
site.auth.basic "",""
//accessToken = "8cab2bd9-d55b-4cb9-bf11-73545917bc57"
site.get(path: '/ssp/api/1/person',
contentType: JSON,
query: ["limit": 1],
headers: ['Authorization': "Bearer ${accessToken}"]) { resp, reader ->
System.out << "Person response: " << reader
println()
}
the YAML is below:
swagger: '2.0'
info:
description: TST
version: 1.0.0
title: APIs - TST
host: ms.foo.com.br
tags:
- name: Send TST object
description: 'TST'
schemes:
- https
paths:
'/tst/templates/V1/{id}/message':
post:
tags:
- Sendo obj
summary: Send obj
description: 'TST '
consumes:
- application/json
produces:
- application/json
parameters:
- name: id
in: path
description: "Object ID"
required: true
type: string
- name: body
in: body
schema:
$ref: '#/definitions/TSTTemplateMessage'
responses:
'200':
description: OK
schema:
$ref: '#/definitions/TSTTemplateResponseMessage'
'401':
description: NOK 1
schema:
$ref: '#/definitions/Unauthorized'
'450':
description: NOK 2
schema:
$ref: '#/definitions/Error'
'550':
description: NOK 3
schema:
$ref: '#/definitions/Error'
security:
- sms_templates_message: []
securityDefinitions:
sms_templates_message:
type: oauth2
tokenUrl: 'https://ms.foo.com.br/tst/templates/V1/id/message/oauth2/token'
flow: application
scopes: {}
definitions:
SMSTemplateMessage:
type: object
description: Entity that represents the object
properties:
id:
type: string
description: Object Identifier
example: "212db47-711-7589-4a56-123"
numberCell:
type: string
description: Attribute 1
example: "551111111111"
templateParameters:
type: array
description: Set of values
items:
type: object
properties:
name:
type: string
value:
type: string
example:
- name: name
value: "John"
- name: date
value: "12/12/2012"
- name: plan
value: "tst plan"
- name: order
value: "1234"
trackingId:
type: string
description: aaaaa
example: "9012909"
required:
- numberCell
SMSGeneralMessage:
type: object
description: aaaaa
properties:
from:
type: string
description: aaaaa
example: "54321"
to:
type: string
description: aaa
example: "55434344111"
textMessage:
type: string
description: aaaa
example: "Jaaaa"
requestDate:
type: string
description: "aaa"
example: "20/04/2012 00:30:00"
required:
- from
- to
- textMessage
SMSTemplateResponseMessage:
type: object
description: aaaa
properties:
templateMessage:
$ref: '#/definitions/SMSTemplateMessage'
sentMessage:
$ref: '#/definitions/SMSGeneralMessage'
Error:
type: object
properties:
message:
type: string
detail:
type: string
providerDetail:
$ref: '#/definitions/ProviderDetail'
ProviderDetail:
type: object
properties:
name:
type: string
code:
type: string
message:
type: string
Unauthorized:
type: object
properties:
error_description:
type: string
example: The access token is invalid or has expired
error:
type: string
example: invalid_token
externalDocs:
description: Find out more about Swagger
url: 'http://swagger.io'
Felipe