Zig
Zig
Create Google Photos Media Item using Upload Token
See more Google Photos Examples
This is the 2nd step in uploading a photo to a Google Photos album. The 1st step is to upload the media data to get an upload token. This step is to use the upload token to add the media item to an album.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Get the previously obtained access token.
// See Get Google Photos Access Token.
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/googlePhotos.json") catch {
std.debug.print("{s}\n", .{try json_token.getLastErrorText(alloc)});
return;
};
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// For more information, see Google Photos Upload Media REST API Documentation
// Build a JSON body that looks like this:
// {
// "albumId": "ALBUM_ID",
// "newMediaItems": [
// {
// "description": "ITEM_DESCRIPTION",
// "simpleMediaItem": {
// "uploadToken": "UPLOAD_TOKEN"
// }
// }
// , ...
// ]
// }
// Use this online tool to generate the code from sample JSON:
// Generate Code to Create JSON
// Build the above JSON:
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("albumId", "ALBUM_ID") catch {};
json.updateString("newMediaItems[0].description", "Photo of Penguins") catch {};
json.updateString("newMediaItems[0].simpleMediaItem.uploadToken", "UPLOAD_TOKEN") catch {};
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// Examine the response status code. Success is indicated by a status code of 200.
std.debug.print("response status code: {d}\n", .{resp.getStatusCode()});
json.load(try resp.getBodyStr(alloc)) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Sample response:
// {
// "newMediaItemResults": [
// {
// "uploadToken": "CAISiQMASsyg4M/x ... CIKFRzD3l8OpFJMRzMsd00D5w",
// "status": {
// "message": "OK"
// },
// "mediaItem": {
// "id": "AKcbugGwtCih2tdl3s1-NMvZFaco3W7XnRwvwtj02J9DJyl6JmrLZDVUFcxQl4AT04LaNrPpsrojeTsDSzOVOF5IgKefD0Y-MQ",
// "description": "Photo of Penguins",
// "productUrl": "https://photos.google.com/lr/photo/AKcbugGwtCih2tdl3s1-NMvZFaco3W7XnRwvwtj02J9DJyl6JmrLZDVUFcxQl4AT04LaNrPpsrojeTsDSzOVOF5IgKefD0Y-MQ",
// "mimeType": "image/jpeg",
// "mediaMetadata": {
// "creationTime": "2008-02-18T05:07:31Z",
// "width": "1024",
// "height": "768"
// },
// "filename": "penguins.jpg"
// }
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var i: i32 = 0;
var upload_token: [:0]const u8 = "";
var status_message: [:0]const u8 = "";
var media_item_id: [:0]const u8 = "";
var media_item_description: [:0]const u8 = "";
var media_item_product_url: [:0]const u8 = "";
var media_item_mime_type: [:0]const u8 = "";
var media_item_media_metadata_creation_time: [:0]const u8 = "";
var media_item_media_metadata_width: [:0]const u8 = "";
var media_item_media_metadata_height: [:0]const u8 = "";
var media_item_filename: [:0]const u8 = "";
i = 0;
const count_i = json.sizeOfArray("newMediaItemResults");
while (i < count_i) {
json.setI(i);
upload_token = try json.stringOf(alloc, "newMediaItemResults[i].uploadToken");
status_message = try json.stringOf(alloc, "newMediaItemResults[i].status.message");
media_item_id = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.id");
media_item_description = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.description");
media_item_product_url = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.productUrl");
media_item_mime_type = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.mimeType");
media_item_media_metadata_creation_time = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.mediaMetadata.creationTime");
media_item_media_metadata_width = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.mediaMetadata.width");
media_item_media_metadata_height = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.mediaMetadata.height");
media_item_filename = try json.stringOf(alloc, "newMediaItemResults[i].mediaItem.filename");
i = i + 1;
}
}