Zig
Zig
Twitter OAuth -- Tweet to Your Own Account
See more HTTP Examples
Demonstrates how to send a tweet (status update) to your own Twitter account using pre-known credentials, which includes:
- Consumer Key
- Consumer Secret
- Access Token
- Token Secret
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();
http.setOAuth1(true);
http.setOAuthVerifier("");
http.setOAuthConsumerKey("my-consumer-key");
http.setOAuthConsumerSecret("my-consumer-secret");
http.setOAuthToken("my-access-token");
http.setOAuthTokenSecret("my-token-secret");
// Send the same status update as shown in this example:
// https://dev.twitter.com/docs/api/1.1/post/statuses/update
// IMPORTANT: Make sure this app has read/write access.
// Otherwise it cannot post an update (i.e. tweet) to the Twitter account.
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.addParam("status", "Maybe he'll finally find his keys. #peterfalk");
req.setHttpVerb("POST");
req.setContentType("application/x-www-form-urlencoded");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.twitter.com/1.1/statuses/update.json", req, resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
if (resp.getStatusCode() == 200) {
// Display the JSON response.
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
} else {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
}
}