Skip to content

Commit

Permalink
Exemplos em JS e PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
guicouto committed Sep 12, 2018
1 parent 43762e6 commit d2ab9dd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/exemplo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//Exemplo básico utilizando a linguagem JAVASCRIPT

var url = "https://api.dev.hubsoft.com.br/oauth/token";
var data = {
"grant_type":"password",
"client_id":"3",
"client_secret":"ONe7Ns48Y30tBMzneW",
"username":"api@hubsoft.com.br",
"password":"api123api"
};

$.ajax({
type: "POST",
url: url,
data: data,
success: success,
error: error
});

function success(response){
var alerta = 'SUCESSO ---- TOKEN TYPE: ' + response.token_type + ' | ACCESS TOKEN: ' + response.access_token;

alert(alerta);
}

function error(){
alert('error');
}
61 changes: 61 additions & 0 deletions examples/exemplo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* O código abaixo é apenas um exemplo para facilitar o desenvolvimento
* Em ambiente de produção, o ideal é que faça a separação das funções
* e se possível faça o desenvolvimento com orientação a objetos.
*/

function requisicao($url, $method, $body = [], $token = null){
$req = curl_init($url);
$header = array();
$header[] = 'Accept: application/json';
if(!is_null($token)){
$header[] = 'Authorization: ' . $token;
}

curl_setopt($req, CURLOPT_HTTPHEADER, $header);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);

if($method == "POST"){
curl_setopt($req, CURLOPT_POST, true );
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($body));
}

$respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
$resp = json_decode(curl_exec($req), true);
curl_close($req);

return $resp;
}

$clientId = "3";
$clientSecret = "ONe7Ns48Y30tBMzneWAwL6hWuh4ze09Jf7qcMsO9";
$username = "api@hubsoft.com.br";
$password = "api123api";
$url = "https://api.dev.hubsoft.com.br";
$urlOauth = $url . "/oauth/token";
$urlCliente = $url . "/api/v1/integracao/cliente?busca=cpf_cnpj&termo_busca=09141806654";

//Body da requisição do oauth
$requestBody = [
"client_id"=>$clientId,
"client_secret"=>$clientSecret,
"username"=>$username,
"password"=>$password,
"grant_type"=>"password"
];

//Faz autorização do oauth
$reqOauth = requisicao($urlOauth, 'POST', $requestBody);

//Monta o token Authorization
$tokenType = $reqOauth['token_type'];
$accessToken = $reqOauth['access_token'];
$authorizationToken = $tokenType . " " . $accessToken;

//Faz requisição do cliente
$reqCliente = requisicao($urlCliente, 'GET', [], $authorizationToken);

var_dump($reqCliente);

0 comments on commit d2ab9dd

Please sign in to comment.