O token de aplicação serve para acessos a API para integração com organizações, do qual é necessário para criar um fluxo utilizando no página para montar o documento usando templates. Para isso, as credenciais utilizadas para gerar este token devem ser uma conta do tipo PJ.
Para saber se sua conta é PJ usando os endpoints da API, podemos fazer da seguinte maneira:
curl -X GET \-H "Authorization: Bearer 41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4" \"https://api.assine.online/v1/user?filter[0][field]=id&filter[0][type]=eq&filter[0][value]=\$USER_ID"
const token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';const params = 'filter[0][field]=id&filter[0][type]=eq&filter[0][value]=$USER_ID';const response = await fetch(`https://api.assine.online/v1/user?${params}`, {method: 'GET',headers: {Authorization: `Bearer ${token}`}});const data = await response.json();
$token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';$query = ['filter[0][field]' => 'id','filter[0][type]' => 'eq','filter[0][value]' => '$USER_ID'];$client = new GuzzleHttp\Client();$res = $client->request('GET', 'https://api.assine.online/v1/user', ['headers' => ['Authorization' => 'Bearer ' . $token],'query' => $query]);echo $res->getBody();
GET https://api.assine.online/v1/user
Não altere o $USER_ID
da url, este converte internamente para o id do usuário correspondente ao token de acesso informado no header Authorization
.
Resposta:
{"_links": {"self": {"href": "https://api.assine.online/v1/user?filter%5B0%5D%5Bfield%5D=id&filter%5B0%5D%5Btype%5D=eq&filter%5B0%5D%5Bvalue%5D=$USER_ID&page=1"},"first": {"href": "https://api.assine.online/v1/user?filter%5B0%5D%5Bfield%5D=id&filter%5B0%5D%5Btype%5D=eq&filter%5B0%5D%5Bvalue%5D=$USER_ID"},"last": {"href": "https://api.assine.online/v1/user?filter%5B0%5D%5Bfield%5D=id&filter%5B0%5D%5Btype%5D=eq&filter%5B0%5D%5Bvalue%5D=$USER_ID&page=1"}},"_embedded": {"user": [{"id": 474,"username": "siletic225@xhyemail.com","status": "1","name": "Jhon Doe","email": "siletic225@xhyemail.com","cellphone": "+5562991838359","document": "12312312312","country": "Brazil","state": "GO","city": "Goiânia","address": "Av 136, Ed. New York Square 19/20","zipCode": "74000000","dateCreated": {"date": "2020-02-20 13:28:28.000000","timezone_type": 3,"timezone": "UTC"},"dateLastUpdated": {"date": "2020-02-20 13:29:10.000000","timezone_type": 3,"timezone": "UTC"},"_embedded": {"businessUnit": {"id": 24,"name": "Soluti Soluções em negocios inteligentes","document": "cnpj","status": null,"country": "Brazil","state": "GO","city": "Goiânia","address": "Av 136, Ed. New York Square 19/20","zipCode": "74000000","_links": {"self": {"href": "https://api.assine.online/v1/business-unit/24"}}}},"_links": {"self": {"href": "https://api.assine.online/v1/user/474"}}}]},"page_count": 1,"page_size": 25,"total_items": 1,"page": 1}
Caso exista configurações da sua organização na chave businessUnit
então esta conta está habilitada para gerar tokens de aplicação.
Para gerar o token de aplicação, é necessário informar a senha da conta para validar a requisição, bem como uma data de validade para este token, lembrando que é de suma importância que manipule este token de forma segura para que não corra o risco de pessoas não autorizadas terem acesso a ele. Caso tenha suspeitas de que o token possa ter sido comprometido, revogue-o. Para gerar o token:
curl -X POST \-H "Authorization: Bearer 41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4" \-H "Content-Type: application/json" \https://api.assine.online/v1/access-token -d '{"password": "INFORME_SENHA","expires": "2021-01-01 23:59:00"}'
const token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';const body = {password: 'INFORME_SENHA',expires: '2021-01-01 23:59:00'};const response = await fetch('https://api.assine.online/v1/access-token', {method: 'POST',headers: {Authorization: `Bearer ${token}`'Content-Type': 'application/json'},body: JSON.stringify(body)});const data = await response.json();
$token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';$client = new GuzzleHttp\Client();$res = $client->request('POST', 'https://api.assine.online/v1/access-token', ['headers' => ['Authorization' => 'Bearer ' . $token],'json' => ['password' => 'INFORME_SENHA','expires' => '2021-01-01 23:59:00']]);echo $res->getBody();
POST https://api.assine.online/v1/access-token
Resposta:
{"accessToken": "5499d5cac5d19926adcbcda7987c01854065554f","expires": {"date": "2021-01-01 23:59:00.000000","timezone_type": 3,"timezone": "UTC"},"id": "5347?q=verify&sessionIndex=a18443cb74060f334d1530c01fa21f12c92b19e5","type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title": "Token created","status": 201,"detail": "Access Token data"}
Guarde este token pois este é o único momento que ele será exibido, caso perca este token, é necessário que gere outro.
Para listar token de aplicação gerados:
curl -X GET \-H "Authorization: Bearer 41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4" \https://api.assine.online/v1/access-token
const token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';const response = await fetch('https://api.assine.online/v1/access-token', {method: 'GET',headers: {Authorization: `Bearer ${token}`}});const data = await response.json();
$token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';$client = new GuzzleHttp\Client();$res = $client->request('GET', 'https://api.assine.online/v1/access-token', ['headers' => ['Authorization' => 'Bearer ' . $token]]);echo $res->getBody();
GET https://api.assine.online/v1/access-token
Resposta:
{"_links": {"self": {"href": "https://api.assine.online/access-token?page=1"},"first": {"href": "https://api.assine.online/access-token"},"last": {"href": "https://api.assine.online/access-token?page=1"}},"_embedded": {"access_token": [{"accessToken": "***** HIDDEN *****","expires": {"date": "2020-02-21 13:29:31.000000","timezone_type": 3,"timezone": "UTC"},"id": "5320","scope": {},"_embedded": {"user": {"id": 474,"name": "Jhon Doe","username": "siletic225@xhyemail.com","email": "siletic225@xhyemail.com","_links": {"self": {"href": "https://api.assine.online/user/474"}}}},"_links": {"self": {"href": "https://api.assine.online/access-token/5320"}}},{"accessToken": "***** HIDDEN *****","expires": {"date": "2021-01-01 23:59:00.000000","timezone_type": 3,"timezone": "UTC"},"id": "5347","scope": {},"_embedded": {"user": {"id": 474,"name": "Jhon Doe","username": "siletic225@xhyemail.com","email": "siletic225@xhyemail.com","_links": {"self": {"href": "https://api.assine.online/user/474"}}}},"_links": {"self": {"href": "https://api.assine.online/access-token/5347"}}}]},"page_count": 1,"page_size": 25,"total_items": 3,"page": 1}
Veja que o primeiro token corresponde ao nosso próprio token de sessão do qual foi gerado quando fizemos login, e o segundo é o token de aplicação que pedimos para ser gerado.
Para revogar um token de aplicação, pegue o id
do token do qual é exibido ao fazer a listagem dos tokens e execute:
curl -X DELETE \-H "Authorization: Bearer 41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4" \https://api.assine.online/v1/access-token/5347
const token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';const tokenId = 5347;const response = await fetch(`https://api.assine.online/v1/access-token/${tokenId}`, {method: 'DELETE',headers: {Authorization: `Bearer ${token}`}});const data = await response.json();
$token = '41f7ff53c1dc5cf4f3db1f33e026b2908cdf88b4';$tokenId = 5347;$client = new GuzzleHttp\Client();$res = $client->request('DELETE', 'https://api.assine.online/v1/access-token/' . $tokenId, ['headers' => ['Authorization' => 'Bearer ' . $token]]);echo $res->getBody();
DELETE https://api.assine.online/v1/access-token/:id
Deve ser retornado status 204
da requisição simbolizando que o token foi revogado.