Java
Chilkat2 Java
hacienda.go.cr Refrescar un Token
See more hacienda.go.cr Examples
Refreshes an access token for the Recepción de Comprobantes Electrónicos del Ministerio de Hacienda (Costa Rica)Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// saved by this example: Get hacienda.co.cr OAuth2 Access Token
JsonObject jsonToken = new JsonObject();
try {
jsonToken.loadFile("qa_data/tokens/hacienda_cr.json");
} catch (ChilkatException e) {
System.out.println("Failed to load constantContact.json");
return;
}
// The access token JSON looks like this:
// {
// "access_token": "ey....",
// "expires_in": 300,
// "id_token": "ey....",
// "not-before-policy": 0,
// "refresh_expires_in": 1800,
// "refresh_token": "ey...",
// "session_state": "...",
// "token_type": "bearer"
// }
Http http = new Http();
// Implements the following CURL command:
// curl -X "POST" "https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token" \
// -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \
// --data-urlencode "client_id=api-stag" \
// --data-urlencode "refresh_token=ey..." \
// --data-urlencode "grant_type=refresh_token"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
HttpRequest req = new HttpRequest();
req.setHttpVerb("POST");
req.setPath("/auth/realms/rut-stag/protocol/openid-connect/token");
req.setContentType("application/x-www-form-urlencoded");
req.addParam("client_id", "api-stag");
req.addParam("refresh_token", jsonToken.stringOf("refresh_token"));
req.addParam("grant_type", "refresh_token");
HttpResponse resp = new HttpResponse();
try {
http.httpReq("https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token", req, resp);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
StringBuilder sbResponseBody = new StringBuilder();
resp.getBodySb(sbResponseBody);
JsonObject jResp = new JsonObject();
jResp.loadSb(sbResponseBody);
jResp.setEmitCompact(false);
System.out.println("Response Body:");
System.out.println(jResp.emit());
int respStatusCode = resp.getStatusCode();
System.out.println("Response Status Code = " + respStatusCode);
if (respStatusCode >= 400) {
System.out.println("Response Header:");
System.out.println(resp.getHeader());
System.out.println("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "access_token": "ey....",
// "expires_in": 300,
// "id_token": "ey....",
// "not-before-policy": 0,
// "refresh_expires_in": 1800,
// "refresh_token": "ey...",
// "session_state": "...",
// "token_type": "bearer"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
String access_token = jResp.stringOf("access_token");
int expires_in = jResp.intOf("expires_in");
String id_token = jResp.stringOf("id_token");
int not_before_policy = jResp.intOf("not-before-policy");
int refresh_expires_in = jResp.intOf("refresh_expires_in");
String refresh_token = jResp.stringOf("refresh_token");
String session_state = jResp.stringOf("session_state");
String token_type = jResp.stringOf("token_type");
// Save the new JSON access token response to a file.
jResp.writeFile("qa_data/tokens/haciendda_cr.json");
}
}