Sample code for 30+ languages & platforms
Zig

ETrade Renew Access Token

See more ETrade Examples

Renews an ETrade OAuth access token.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setOAuth1(true);
    http.setOAuthVerifier("");
    http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
    http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 Authorization
    const json_token = try chilkat.JsonObject.init();
    defer json_token.deinit();
    json_token.loadFile("qa_data/tokens/etrade.json") catch {
        std.debug.print("Failed to load OAuth1 token\n", .{});
        return;
    };

    http.setOAuthToken(try json_token.stringOf(alloc, "oauth_token"));
    http.setOAuthTokenSecret(try json_token.stringOf(alloc, "oauth_token_secret"));

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("GET", "https://api.etrade.com/oauth/renew_access_token", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Make sure a successful response was received.
    if (resp.getStatusCode() != 200) {
        std.debug.print("{s}\n", .{try resp.getStatusLine(alloc)});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
        return;
    }

    // If successful, the resp.BodyStr contains something like this:
    // oauth_token=%3TiQRgQCRGPo7Xdk6G8QDSEzX0Jsy6sKNcULcDavAGgU%3D&oauth_token_secret=%7RrC9scEpzcwSEMy4vE7nodSzPLqfRINnTNY4voczyFM%3D
    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    const sb_resp_body = try chilkat.StringBuilder.init();
    defer sb_resp_body.deinit();
    resp.getBodySb(sb_resp_body) catch {};
    if (sb_resp_body.contentsEqual("Access Token has been renewed", false)) {
        // The documentation at https://apisb.etrade.com/docs/api/authorization/renew_access_token.html
        // indicates that the response should be as described above.  However, the response received when
        // trying to refresh a non-expired token was "Access Token has been renewed"
        std.debug.print("Keeping the same access token, but it's renewed...\n", .{});
        return;
    }

    const hash_tab = try chilkat.Hashtable.init();
    defer hash_tab.deinit();
    hash_tab.addQueryParams(try resp.getBodyStr(alloc)) catch {};

    const access_token = try hash_tab.lookupStr(alloc, "oauth_token");
    const access_token_secret = try hash_tab.lookupStr(alloc, "oauth_token_secret");

    // The access token + secret is what should be saved and used for
    // subsequent REST API calls.
    std.debug.print("Access Token = {s}\n", .{access_token});
    std.debug.print("Access Token Secret = {s}\n", .{access_token_secret});

    // Save this access token for future calls.
    // Just in case we need user_id and screen_name, save those also..
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.appendString("oauth_token", access_token) catch {};
    json.appendString("oauth_token_secret", access_token_secret) catch {};

    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    fac.writeEntireTextFile("qa_data/tokens/etrade.json", try json.emit(alloc), "utf-8", false) catch {};

    std.debug.print("Success.\n", .{});
}