-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|