Zig
Zig
X.com App only authentication and OAuth 2.0 Bearer Token
See more X Examples
Demonstrates how to obtain an X.com OAuth2 token for application-only authentication.This allows you to issue authenticated requests on behalf of the application itself (as opposed to on behalf of a specific user). You don’t have the context of an authenticated user and this means that any request to API for endpoints that require user context, such as posting Tweets, will not work.
Your app will be able to, for example:
- Pull user timelines;
- Access friends and followers of any account;
- Access lists resources;
- Search in Tweets;
And it will not be able to:
- Post Tweets or other resources;
- Connect to Streaming endpoints;
- Search for users;
- Use any geo endpoint;
- Access Direct Messages or account credentials;
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// It requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Update these values with your consumer key and secret (also known as API Key and API Key Secret).
const consumer_key = "X_CONSUMER_KEY";
const consumer_secret = "X_CONSUMER_SECRET";
const http = try chilkat.Http.init();
defer http.deinit();
http.setBasicAuth(true);
http.setLogin(consumer_key);
http.setPassword(consumer_secret);
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.addParam("grant_type", "client_credentials");
req.setHttpVerb("POST");
req.setContentType("application/x-www-form-urlencoded");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.x.com/oauth2/token", req, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
std.debug.print("Response Status Code: {d}\n", .{resp.getStatusCode()});
if (resp.getStatusCode() != 200) {
std.debug.print("Expected a 200 response status code for success.\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
return;
}
// We should get a JSON response like this:
// {
// "token_type":"bearer",
// "access_token":"..."
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.load(try resp.getBodyStr(alloc)) catch {};
std.debug.print("Access token = {s}\n", .{try json.stringOf(alloc, "access_token")});
}