Sample code for 30+ languages & platforms
Rust

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 Rust Downloads

Rust

// 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.

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/googlePhotos.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

let http = chilkat::Http::new();

http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

// 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:
let json = chilkat::JsonObject::new();
let _ = json.update_string("albumId", "ALBUM_ID");
let _ = json.update_string("newMediaItems[0].description", "Photo of Penguins");
let _ = json.update_string("newMediaItems[0].simpleMediaItem.uploadToken", "UPLOAD_TOKEN");

let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate", &json, "application/json", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

// Examine the response status code.  Success is indicated by a status code of 200.
println!("response status code: {}", resp.status_code());

let _ = json.load(&resp.body_str());
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());

// 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

let mut i: i32 = 0;

i = 0;
let count_i = json.size_of_array("newMediaItemResults");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("newMediaItemResults[i].uploadToken").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].status.message").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.id").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.description").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.productUrl").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.mimeType").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.mediaMetadata.creationTime").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.mediaMetadata.width").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.mediaMetadata.height").unwrap_or_default();
    let _ = json.string_of("newMediaItemResults[i].mediaItem.filename").unwrap_or_default();
    i = i + 1;
}