Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Create Google Photos Media Item using Upload Token

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

ActiveX for 32-bit and 64-bit Windows

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.