CPP 145
OneDrive -- List Drives Guest on 5th June 2020 10:39:00 AM
  1. #include <CkHttp.h>
  2. #include <CkJsonObject.h>
  3.  
  4. void ChilkatSample(void)
  5.     {
  6.     // This example requires the Chilkat API to have been previously unlocked.
  7.     // See Global Unlock Sample for sample code.
  8.  
  9.     CkHttp http;
  10.  
  11.     // Use your previously obtained access token here:
  12.     // See the following examples for getting an access token:
  13.     //    Get Microsoft Graph OAuth2 Access Token (Azure AD v2.0 Endpoint).
  14.     //    Get Microsoft Graph OAuth2 Access Token (Azure AD Endpoint).
  15.     //    Refresh Access Token (Azure AD v2.0 Endpoint).
  16.     //    Refresh Access Token (Azure AD Endpoint).
  17.  
  18.     // (Make sure your token was obtained with the FilesRead or Files.ReadWrite scope.)
  19.     http.put_AuthToken("MICROSOFT_GRAPH_ACCESS_TOKEN");
  20.  
  21.     // Sends the following GET request:
  22.     // GET https://graph.microsoft.com/v1.0/me/drives
  23.  
  24.     const char *resp = http.quickGetStr("https://graph.microsoft.com/v1.0/me/drives");
  25.     if (http.get_LastMethodSuccess() != true) {
  26.         std::cout << http.lastErrorText() << "\r\n";
  27.         return;
  28.     }
  29.  
  30.     // The response should be JSON.
  31.     CkJsonObject json;
  32.     json.put_EmitCompact(false);
  33.     json.Load(resp);
  34.  
  35.     // A successful response should return a status code of 200.
  36.     if (http.get_LastStatus() != 200) {
  37.         std::cout << json.emit() << "\r\n";
  38.         std::cout << "Response status = " << http.get_LastStatus() << "\r\n";
  39.         return;
  40.     }
  41.  
  42.     std::cout << json.emit() << "\r\n";
  43.  
  44.     // Sample output:
  45.  
  46.     //  {
  47.     //    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives",
  48.     //    "value": [
  49.     //      {
  50.     //        "id": "3a33fceb9b74cc15",
  51.     //        "driveType": "personal",
  52.     //        "owner": {
  53.     //          "user": {
  54.     //            "displayName": "Matt Smith",
  55.     //            "id": "3a33fceb9b74cc15"
  56.     //          }
  57.     //        },
  58.     //        "quota": {
  59.     //          "deleted": 0,
  60.     //          "remaining": 1114571275977,
  61.     //          "state": "normal",
  62.     //          "total": 1115617755136,
  63.     //          "used": 1046479159
  64.     //        }
  65.     //      }
  66.     //    ]
  67.     //  }
  68.  
  69.     // Iterate over the drives in the JSON response:
  70.     int i = 0;
  71.     int numDrives = json.SizeOfArray("value");
  72.     while (i < numDrives) {
  73.         json.put_I(i);
  74.         std::cout << "-- Drive " << (i + 1) << "\r\n";
  75.         std::cout << "id: " << json.stringOf("value[i].id") << "\r\n";
  76.         // The size will be a number larger than what can fit in a 32-bit integer.
  77.         // Get the integer as a string..
  78.         std::cout << "total: " << json.stringOf("value[i].quota.total") << "\r\n";
  79.         i = i + 1;
  80.     }
  81.     }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.