Objective-C Google Drive: List Files

Back to Index

Lists files..

Documentation: https://developers.google.com/drive/v3/reference/files/list


#import <CkoRest.h>
#import <CkoOAuth2.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//   Provide a previously obtained OAuth2 access token.
CkoOAuth2 *oauth2 = [[CkoOAuth2 alloc] init];
oauth2.AccessToken = @"OAUTH2_ACCESS_TOKEN";
[rest SetAuthOAuth2: oauth2];

success = [rest Connect: @"www.googleapis.com" port: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

CkoStringBuilder *sbJson = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/drive/v3/files" sb: sbJson];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"Received error response code: ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@",@"Response body:");
    NSLog(@"%@",[sbJson GetAsString]);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json LoadSb: sbJson];

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.
NSString *kind = 0;
BOOL incompleteSearch;
int i;
int count_i;
NSString *id = 0;
NSString *name = 0;
NSString *mimeType = 0;

kind = [json StringOf: @"kind"];
incompleteSearch = [json BoolOf: @"incompleteSearch"];
i = 0;
count_i = [[json SizeOfArray: @"files"] intValue];
while (i < count_i) {
    json.I = [NSNumber numberWithInt: i];
    kind = [json StringOf: @"files[i].kind"];
    id = [json StringOf: @"files[i].id"];
    name = [json StringOf: @"files[i].name"];
    mimeType = [json StringOf: @"files[i].mimeType"];
    i = i + 1;
}

NSLog(@"%@",@"Example Completed.");

Sample JSON Response Body

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolT1E4blNEaHlBVTg",
   "name": "misc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B84ttUI7E_NyWHBYNUlFRGZSWVE",
   "name": "02112017091435553-LW0826-058444946-4C414E43.xml",
   "mimeType": "application/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B5w0YPzxv8g6SmRDTWUweEJCeVk",
   "name": "Sales in 2004.pdf",
   "mimeType": "application/pdf"
  },
  {
   "kind": "drive#file",
   "id": "0By-hrxCCDzSQUGxJY0hzNUFpNmc",
   "name": "helloWorld.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolMjZhSkV0a2tUX3c",
   "name": "hamletC.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVlA1d0tscUtRLXc",
   "name": "chilkat",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolelZ2UGdPZmlJTmc",
   "name": "hamlet2.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolaWNqLWJqLUhwb2M",
   "name": "starfish3.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolNlFnbDhWZ3g3UGc",
   "name": "starfish4.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolQlExSlBQT1phZXM",
   "name": "pigs.json",
   "mimeType": "attachment/json"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVHRPVkxtYWFtZkk",
   "name": "starfish20.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolRGZEV3ZGUTZfNFk",
   "name": "penguins2.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolS2FXSjliMXQxSU0",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolREhVUzVWRUlFeGM",
   "name": "Пингвины.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolZUhxckMzb0dRMzg",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbUF6WS1Gei1oalk",
   "name": "penguins.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYola296ODZUSm5GYU0",
   "name": "123",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbTE3c3J5RHBUcHM",
   "name": "abc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolTmhybWJSUGd5Q2c",
   "name": "testHello.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolY2tPU1BnYW02T2c",
   "name": "testFolder",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}