Search code examples
c#google-drive-apigoogle-admin-sdkgoogle-reporting-api

How to get metadata of an unshared file from another user with google drive api?


I'm currently working on a C# asp.net mvc web app using the Google Reports API from Admin SDK to receive notifications when something happens on my Google Drive.

Then, I need to use in addition the Drive REST API GET Request to build the file Path (and more) by using the the doc_id given by the Reports API.


Everything is working fine, except when the Report API notifies me with another user unshared file. The Drive REST API is trying to find the file with no success.

Here is the Error 404 returned:

Google.Apis.Requests.RequestError
File not found: 0B48ytfDI5h8qY2k4VzdjMkZRNXM [404]
Errors [
    Message[File not found: 0B48ytfDI5h8qY2k4VzdjMkZRNXM] Location[file - other] Reason[notFound] Domain[global]
] 

I'm using this scope : DriveService.Scope.Drive

The file owner is able to see metadata of his file by using this form, and I still have the Error 404.

I'm the drive administrator and on my Google Apps Admin Settings, I've tried different configs with no success, even with the Default Shared Link on Everyone from the domain.


So my question is: Is that possible to get file meta data when you are not the file owner and the file isn't shared ?

PS: I'm using a Google Apps for work unlimited in relation with my work domain, so I thought I could at least get metadata files as an admin even if the file is not shared.


Solution

  • To my knowledge, the only way to access the user's file in Drive API is through setting up a Service Account for your web application. You can read more about this in the Drive API documentation under Domain-Wide Delegation.

    To give a brief summary:

    Service Account is an account that belongs to your application instead of to an individual end user. This allows your application to have authority to make API calls as users in your domain (also to "impersonate" users).

    To get started, the following tasks have to be performed:

    1. Create or select a project in the Google Developers console and enable the API (e.g. Drive API).
    2. Go to Credentials, add new credentials and select Service Account. More details here.
    3. This will create three items (client ID, private key file, and email address), which is required in authorizing your application in the Google Apps domain's Admin Console.

    Now, the service account that you have created needs to be granted access to the Google Apps domain's user data that you want access. This will require the administrator to authorize the service account in the Admin console. Since it's already provided in the documentation, you can access the appropriate steps in the document.

    Now, your application can instantiate an authorized Drive service Object (i.e. metadata) on behalf of your Google Apps domain's users. Hope this helps and Good luck!