Drive API 404 Error

1,341 views
Skip to first unread message

Sergei Kvasov

unread,
Sep 14, 2020, 8:53:29 AM9/14/20
to Google APIs Client Library for Objective-C
Hi guys, 

I'm working on iOS app to manage Google Drive. Previously the app were using GoogleAPIClient/Drive library to access Google Drive, but now the library is deprecated and I'm trying to replace it with a new one  - GoogleAPIClientForREST. After successful login to my Google Account with GoogleSignIn library I'm trying to make search request to Google Drive with GoogleAPIClientForREST. After any request I get an error: An error occurred: Error Domain=com.google.HTTPStatus Code=404 "Not Found". No other details in log. Any idea?

Below code of my request:

- (void) searchRootFolder: (CompletionSearch) completionSearch

{

    NSString *parentId = @"root";

    

    GTLRDriveQuery_FilesList *query = [GTLRDriveQuery_FilesList query];

    query.q = [NSString stringWithFormat:@"'%@' in parents and trashed != true and '%@' in owners and name = '%@'", parentId, self.client.authenticator.usermail, [NSBundle appName]];

    query.orderBy = @"modifiedTime desc";

    query.pageSize = 1000;

    query.fields = kQueryFieldsFolder;

    

    [[self driveService] executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,

                                                                GTLRDrive_FileList *fileList,

                                                                NSError *error) {

        if (!error)

        {

            NSLog(@"Drive search root folder results - %li", (unsigned long)fileList.files.count);

            

            NSString * newRootId = [fileList.files.firstObject identifier];

            

            if (newRootId.length > 0)

            {

                self.rootFolder = [fileList.files.firstObject folder];

                if (completionSearch)

                    completionSearch(YES, nil);

            }

            else

            {

                if (completionSearch)

                    completionSearch(NO, nil);

            }

        }

        else

        {

            if (completionSearch)

                completionSearch(NO, error);

            

            NSLog(@"DRIVE An error occurred: %@", error);

        }

    }];

}

thom...@google.com

unread,
Sep 14, 2020, 8:58:42 AM9/14/20
to Google APIs Client Library for Objective-C
There really isn't enough into to tell from the snippet you have.  I'd suggest turning on HTTP logging, and look at the generated log to try and see exactly what's going out and coming back.  If you are doing any custom configuration of `self.driveService` then it could be your specific values for the service's url that are wrong.

TVL

Sergei Kvasov

unread,
Sep 15, 2020, 7:40:50 AM9/15/20
to Google APIs Client Library for Objective-C
I have enabled logging:

-----------------------------------------------------------
drive.files.list

2020-09-15 10:12:19 +0000  elapsed: 2.971sec
Request headers:
  Accept: application/json
  Authorization: Bearer _snip_
  Cache-Control: no-cache
  Content-Type: application/json; charset=utf-8
  User-Agent: com.snap2cloud.snap2cloudapp/1.0.123 google-api-objc-client/3.0 iPhone/13.7 hw/sim (gzip)

Response: status 404
Response headers:
  Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  Content-Encoding: gzip
  Content-Length: 29
  Content-Type: text/html; charset=UTF-8
  Date: Tue, 15 Sep 2020 10:12:17 GMT
  Expires: Mon, 01 Jan 1990 00:00:00 GMT
  Pragma: no-cache
  Server: GSE
  Vary: Origin, X-Origin
  alt-svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
  content-security-policy: frame-ancestors 'self'
  x-content-type-options: nosniff
  x-frame-options: SAMEORIGIN
  x-xss-protection: 1; mode=block

Response body: (9 bytes)
Not Found
-----------------------------------------------------------

понедельник, 14 сентября 2020 г. в 14:58:42 UTC+2, thom...@google.com:

thom...@google.com

unread,
Sep 15, 2020, 8:30:10 AM9/15/20
to Google APIs Client Library for Objective-C
That URL isn't right.  How is your GTLRDriveService being configured, it would appear that you are overriding some of the configuration and that is what is causing the wrong urls to be created.

TVL

Sergei Kvasov

unread,
Sep 15, 2020, 10:03:51 AM9/15/20
to Google APIs Client Library for Objective-C
Thanks for your quick replies. You are right, GTLRDriveService configuration was wrong, now I use https://www.googleapis.com/drive/v3/ as rootURL and my fetch request works just fine! But I have another issue with file uploading, I'm trying to upload an image to Google Drive folder and I get an error with status 501, request log looks good:

-----------------------------------------------------------
drive.files.create

2020-09-15 13:32:12 +0000  elapsed: 1.475sec
Request headers:
  Accept: application/json
  Authorization: Bearer _snip_
  Cache-Control: no-cache
  Content-Type: application/json; charset=utf-8
  User-Agent: com.snap2cloud.snap2cloudapp/1.0.123 google-api-objc-client/3.0 iPhone/13.7 hw/sim (gzip) (GTMSUF/1)
  X-Goog-Upload-Command: start
  X-Goog-Upload-Content-Length: 6479
  X-Goog-Upload-Content-Type: image/jpeg
  X-Goog-Upload-Protocol: resumable

Request body: (87 bytes)
{
  "parents" : [
    "1zaCaa0GSmZqY-50MUC6bwVa9gA6C0LHk"
  ],
  "name" : "Snap2Clouds1531044092020.jpg"
}

Response: status 200
Response headers:
  Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  Content-Encoding: gzip
  Content-Length: 142
  Content-Type: application/json; charset=UTF-8
  Date: Tue, 15 Sep 2020 13:32:12 GMT
  Expires: Mon, 01 Jan 1990 00:00:00 GMT
  Pragma: no-cache
  Server: GSE
  Vary: Origin, X-Origin
  alt-svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
  content-security-policy: frame-ancestors 'self'
  x-content-type-options: nosniff
  x-frame-options: SAMEORIGIN
  x-xss-protection: 1; mode=block

Response body: (138 bytes)
{
  "mimeType" : "image\/jpeg",
  "id" : "1Wy-FhQnZaZsV_nzwlsngd1KXg-J2y8i5",
  "kind" : "drive#file",
  "name" : "Snap2Clouds1531044092020.jpg"
}
-----------------------------------------------------------

вторник, 15 сентября 2020 г. в 14:30:10 UTC+2, thom...@google.com:

thom...@google.com

unread,
Sep 15, 2020, 10:07:10 AM9/15/20
to Google APIs Client Library for Objective-C
You should have to set the url at all.  The default service should have the correct root url and servicePath.  There's a few different urls that get assembled from the parts, so forcing the root url to that is going to cause other problems.  Is there a reason you aren't using the defaults done by the generator?

TVL

Sergei Kvasov

unread,
Sep 15, 2020, 10:15:05 AM9/15/20
to Google APIs Client Library for Objective-C
Yes, I get exception if I don't specify root url: rootURLString should end in a slash: (null)

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'rootURLString should end in a slash: (null)'

*** First throw call stack:

(

0   CoreFoundation                      0x00007fff23e3de6e __exceptionPreprocess + 350

1   libobjc.A.dylib                     0x00007fff512a19b2 objc_exception_throw + 48

2   CoreFoundation                      0x00007fff23e3dbe8 +[NSException raise:format:arguments:] + 88

3   Foundation                          0x00007fff258e6c9b -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 166

4   Snap2Clouds                         0x000000010066d7a8 -[GTLRService URLFromQueryObject:usePartialPaths:includeServiceURLQueryParams:] + 2408

5   Snap2Clouds                         0x000000010066fbf7 -[GTLRService executeQuery:completionHandler:] + 3095

6   Snap2Clouds                         0x0000000100513058 -[SCGoogleDriveManager searchRootFolder:] + 680

7   Snap2Clouds                         0x0000000100510b38 -[SCGoogleDriveManager checkRoot:] + 184

8   Snap2Clouds                         0x0000000100510122 -[AppDelegate signIn:didSignInForUser:withError:] + 1202

9   Snap2Clouds                         0x000000010153db9f __37-[GIDSignIn addCallDelegateCallback:]_block_invoke + 105

10  Snap2Clouds                         0x0000000101538f59 -[GIDCallbackQueue fire] + 117

11  Snap2Clouds                         0x000000010153da53 __38-[GIDSignIn addDecodeIdTokenCallback:]_block_invoke_2 + 609

12  Snap2Clouds                         0x0000000100614b53 __71-[GTMSessionFetcher invokeFetchCallbacksOnCallbackQueueWithData:error:]_block_invoke + 51

13  Snap2Clouds                         0x0000000100614843 __66-[GTMSessionFetcher invokeOnCallbackQueue:afterUserStopped:block:]_block_invoke + 515

14  libdispatch.dylib                   0x0000000104342f11 _dispatch_call_block_and_release + 12

15  libdispatch.dylib                   0x0000000104343e8e _dispatch_client_callout + 8

16  libdispatch.dylib                   0x0000000104351e67 _dispatch_main_queue_callback_4CF + 1357

17  CoreFoundation                      0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

18  CoreFoundation                      0x00007fff23d9c3b9 __CFRunLoopRun + 2041

19  CoreFoundation                      0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404

20  GraphicsServices                    0x00007fff38c05bbe GSEventRunModal + 139

21  UIKitCore                           0x00007fff49372964 UIApplicationMain + 1605

22  Snap2Clouds                         0x000000010052e1b0 main + 112

23  libdyld.dylib                       0x00007fff5211c1fd start + 1

24  ???                                 0x0000000000000001 0x0 + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException


вторник, 15 сентября 2020 г. в 16:07:10 UTC+2, thom...@google.com:

thom...@google.com

unread,
Sep 15, 2020, 10:16:18 AM9/15/20
to Google APIs Client Library for Objective-C
Again, how are you configuring the GTLRDriveService?  The generated code already has the correct values and this wouldn't happen: https://github.com/google/google-api-objectivec-client-for-rest/blob/master/Source/GeneratedServices/Drive/GTLRDriveService.m#L36-L41

Sergei Kvasov

unread,
Sep 15, 2020, 10:38:39 AM9/15/20
to Google APIs Client Library for Objective-C
Configuration that lead to exception:
-----------------------------------

- (GTLRService *)driveService

{

    static GTLRService *service = nil;

    

    if (!service)

    {

        service = [[GTLRService alloc] init];

        service.shouldFetchNextPages = NO;

        service.retryEnabled = YES;

    }

    if (!service.authorizer)

    {

        service.authorizer = self.client.authenticator.googleAuthorizer;

    }    

    

    return service;

}

-----------------------------------

and configuration that works fine(file upload/creation works too), diff in red:

-----------------------------------

- (GTLRService *)driveService

{

    static GTLRService *service = nil;

    

    if (!service)

    {

        service = [[GTLRService alloc] init];

        service.shouldFetchNextPages = NO;

        service.retryEnabled = YES;

        service.rootURLString = @"https://www.googleapis.com/";

        service.servicePath = @"drive/v3/";

        service.resumableUploadPath = @"resumable/upload/";

        service.simpleUploadPath = @"upload/";

        service.batchPath = @"batch/drive/v3";

        service.prettyPrintQueryParameterNames = @[ @"prettyPrint" ];

    }

    if (!service.authorizer)

    {

        service.authorizer = self.client.authenticator.googleAuthorizer;

    }    

    

    return service;

}

-----------------------------------
вторник, 15 сентября 2020 г. в 16:16:18 UTC+2, thom...@google.com:

thom...@google.com

unread,
Sep 15, 2020, 10:40:13 AM9/15/20
to Google APIs Client Library for Objective-C
Why are you using GTLRService and not GTLRDriveService?  That's what is causing all of this.

TVL

Sergei Kvasov

unread,
Sep 15, 2020, 10:54:55 AM9/15/20
to Google APIs Client Library for Objective-C
Haha, yes, now I use GTLRDriveService and it works fine! It was not clear for me from the docs what class should I use. I should read docs more carefully. Thanks for help!

вторник, 15 сентября 2020 г. в 16:40:13 UTC+2, thom...@google.com:
Reply all
Reply to author
Forward
0 new messages