Sample code for 30+ languages & platforms
Zig

Test Salesforce OAuth2 Access Token

See more Salesforce Examples

Demonstrates how to make a simple Salesforce REST API call to test a previously obtained 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 example does the following:

    // curl -X GET https://yourInstance.salesforce.com/services/oauth2/userinfo \
    //      -H "Authorization: Bearer YOUR_ACCESS_TOKEN"

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

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    // This example assumes the OAuth2 access token was previously fetched
    // and saved to a file.  See Get SalesForce OAuth2 Access Token via Authorization Flow

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/tokens/_salesforce.json") catch {
        std.debug.print("Failed to load OAuth2 access token.\n", .{});
        return;
    };

    // Here's an example of the JSON:

    // {
    //   "access_token": "00D41000....uLZBpT6",
    //   "refresh_token": "5Aep....25xdGgkrV",
    //   "signature": "cjTbSc5DvcKpaMoRTzuQTJLb1tcMw8LEO01flq4aMD4=",
    //   "scope": "refresh_token id",
    //   "instance_url": "https://d41000000f8a0eak-dev-ed.my.salesforce.com",
    //   "id": "https://login.salesforce.com/id/00D41000000F8A0EAK/005410000....xAAE",
    //   "token_type": "Bearer",
    //   "issued_at": "1738348388166"
    // }

    // Adds the "Authorization: Bearer YOUR_ACCESS_TOKEN" header.
    http.setAuthToken(try json.stringOf(alloc, "access_token"));

    // We want to build the following URL:  https://<instance_id>.salesforce.com/services/oauth2/userinfo
    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();
    sb_url.append(try json.stringOf(alloc, "instance_url")) catch {};
    sb_url.append("/services/oauth2/userinfo") catch {};

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb(try sb_url.getAsString(alloc), sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Response status code = {d}\n", .{http.getLastStatus()});

    const json_response = try chilkat.JsonObject.init();
    defer json_response.deinit();
    json_response.loadSb(sb_response_body) catch {};
    json_response.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json_response.emit(alloc)});

    // The expected JSON response is something like this:

    // {
    //   "sub": "005xxxxxxxxxxxx",
    //   "name": "John Doe",
    //   "preferred_username": "johndoe@example.com",
    //   "email": "johndoe@example.com",
    //   "profile": "https://na85.salesforce.com/005xxxxxxxxxxxx"
    // ...
    // ...
    // }
}