Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Create Google Photos Media Item using Upload TokenThis 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.
Use ChilkatAx-win32.pkg Procedure Test Token Handle hoJsonToken Boolean iSuccess Handle hoHttp Handle hoJson Variant vResp Handle hoResp Integer i Integer iCount_i String sUploadToken String sStatusMessage String sMediaItemId String sMediaItemDescription String sMediaItemProductUrl String sMediaItemMimeType String sMediaItemMediaMetadataCreationTime String sMediaItemMediaMetadataWidth String sMediaItemMediaMetadataHeight String sMediaItemFilename String sTemp1 Integer iTemp1 Boolean bTemp1 // 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. Get Create (RefClass(cComChilkatJsonObject)) To hoJsonToken If (Not(IsComObjectCreated(hoJsonToken))) Begin Send CreateComObject of hoJsonToken End Get ComLoadFile Of hoJsonToken "qa_data/tokens/googlePhotos.json" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoJsonToken To sTemp1 Showln sTemp1 Procedure_Return End Get Create (RefClass(cComChilkatHttp)) To hoHttp If (Not(IsComObjectCreated(hoHttp))) Begin Send CreateComObject of hoHttp End Get ComStringOf Of hoJsonToken "access_token" To sTemp1 Set ComAuthToken Of hoHttp To sTemp1 // 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: Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End Get ComUpdateString Of hoJson "albumId" "ALBUM_ID" To iSuccess Get ComUpdateString Of hoJson "newMediaItems[0].description" "Photo of Penguins" To iSuccess Get ComUpdateString Of hoJson "newMediaItems[0].simpleMediaItem.uploadToken" "UPLOAD_TOKEN" To iSuccess Get ComEmit Of hoJson To sTemp1 Get ComPostJson Of hoHttp "https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate" sTemp1 To vResp If (IsComObject(vResp)) Begin Get Create (RefClass(cComChilkatHttpResponse)) To hoResp Set pvComObject Of hoResp To vResp End Get ComLastMethodSuccess Of hoHttp To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoHttp To sTemp1 Showln sTemp1 Procedure_Return End // Examine the response status code. Success is indicated by a status code of 200. Get ComStatusCode Of hoResp To iTemp1 Showln "response status code: " iTemp1 Get ComBodyStr Of hoResp To sTemp1 Get ComLoad Of hoJson sTemp1 To iSuccess Set ComEmitCompact Of hoJson To False Get ComEmit Of hoJson To sTemp1 Showln sTemp1 Send Destroy of hoResp // 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 Move 0 To i Get ComSizeOfArray Of hoJson "newMediaItemResults" To iCount_i While (i < iCount_i) Set ComI Of hoJson To i Get ComStringOf Of hoJson "newMediaItemResults[i].uploadToken" To sUploadToken Get ComStringOf Of hoJson "newMediaItemResults[i].status.message" To sStatusMessage Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.id" To sMediaItemId Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.description" To sMediaItemDescription Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.productUrl" To sMediaItemProductUrl Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.mimeType" To sMediaItemMimeType Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.mediaMetadata.creationTime" To sMediaItemMediaMetadataCreationTime Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.mediaMetadata.width" To sMediaItemMediaMetadataWidth Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.mediaMetadata.height" To sMediaItemMediaMetadataHeight Get ComStringOf Of hoJson "newMediaItemResults[i].mediaItem.filename" To sMediaItemFilename Move (i + 1) To i Loop End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.