Zig
Zig
Moody's REST API - Get OAuth2 Token
See more Moody's Examples
Demonstrates how to get an OAuth2 access token for the Moody's REST API.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.addParam("grant_type", "password");
req.addParam("scope", "api/ratings api/addin rest");
req.addParam("username", "my_username");
req.addParam("password", "my_password");
// I have no idea of where to get the client_id or client_secret.
// When you create a Moody's App, it only provides an "API Key".
req.addParam("client_id", "my_client_id");
req.addParam("client_secret", "my_client_secret");
req.setHttpVerb("POST");
req.setContentType("application/x-www-form-urlencoded");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.moodys.com/OAuth/Token", req, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("status code = {d}\n", .{resp.getStatusCode()});
const response_body = try resp.getBodyStr(alloc);
std.debug.print("{s}\n", .{response_body});
// Save the JSON to a file for future requests.
if (resp.getStatusCode() == 200) {
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
fac.writeEntireTextFile("qa_data/tokens/moodys.json", try resp.getBodyStr(alloc), "utf-8", false) catch {};
}
}