Leave a Reply to Darrell Sveistrup Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

60 thoughts on “Google Drive API with a Service Account.

  • Russell Graves

    This is nice, but I don’t think it works because “ServiceAccountCredential” does not exist in “Google.Apis.Authentication.OAuth2” it was part of “Google.Apis.Auth.OAuth2” which is obsolete now.

    This is the problem I have been having for some time. I have yet to find a Google Service Account C# example that will actually compile.

    This article says Feb 10, 2015, but have you really tried it with current stuff? I hope I am just an idiot, and that there is something I’m missing here.

    Any input??



    • Administrator
      Linda Lawton

      No input at all. I just ran this and it ran fine. Uploaded 3 files, my usings are

      using System;
      using Google.Apis.Drive.v2;
      using Google.Apis.Auth.OAuth2;
      using System.Threading;
      using Google.Apis.Util.Store;
      using Google.Apis.Services;
      using System.Security.Cryptography.X509Certificates;
      using System.IO;

      I will add this code to the sample code lib asap. I have just been having GitHub learning issues.

  • Vo Son Ha

    Dear Linda Lawton,

    – I try this for my project in my computer, it’s work fine. But when I run on my server (Windows Server 2008 R2) , it show error : “invalid_grant”.
    Can you help me please


  • Sun

    Now, I need a function to do this:
    the service will read my own gmail-box every fifteen minutes.
    so which authentication are applied to me? please…

  • Daria

    Linda, you have such a great research at google drive api. I need to use such application on my website – uploading files to google drive with php. Where I can find such implementation in PHP? I checked your article about Google calendar service account using but got problems doing it at google drive api. Thanks a lot.

  • Joebet

    When i try to upload a file it returns null. even when i tried to edit ServiceAccountEmail with a wrong one it doesnt return nulll in the service.. do you know anything about it?

  • Darrell Sveistrup

    “Using a service account to access Google Drive API can be very useful but it is important to remember that a service account is not you. If you want to be able to access the files it uploads you must grant yourself access to them though the service account.”

    If you create a folder on google drive,
    And give your service access to create new files in that folder.
    Would the new files not have the credentials of the parent folder and you will have access to any created ?

  • pregunton

    it’s great. LindaLawtonDK marvellous guru.

    Any full source sample application using YouTube API C# Using good patterns and practices?

    First, i’m very newbie using google APIs.

    I want create unit test in C# for get (and add) URLs videos to my playlist in YouTube. then, I want use YouTube API.

    I’m confused about authentication methods: Simple Key, OAuth2, Service Account, etc ?.

    Any suggestions about it?

    • Administrator
      Linda Lawton

      wow when did i reach guru status? I should put that on my email signature.

      The best i can do is this Youtube I know it says Google Analytics but some of it is YouTube I was testing and uploaded it but never finished.

      You can use a public key for a lot of the YouTube API, anything that is not public you will need to use OAuth2 for. The YouTube API doesn’t support service account access, unfortunately.

  • Santhosh

    Using service account When I try to access the files from Google drive I am unable to get any file, But when I shared the files to service account email, I am able to get all shared files and folders. Instead of sharing to service account email is their any way to get all file of Google drive, using service account email.

  • Hernán

    Hi Linda. I’ve successfully managed to get php to upload files and list them. I can see they’re there. But, I have no idea how to access them through the regular Google Drive interface. Is it possible to see the service account’s files that way? Is it possible to see them outside the API?

    BTW, I couldn’t have done it without your article, thanks!

    • Hernán

      After a little bit of struggle, I understood how to upload files and see them as a regular user with php:

      1. I created a folder as a regular Google Drive user.
      2. I shared it with the service account email.
      3. I used:
      $parent = new Google_Service_Drive_ParentReference();
      4. I inserted the file.

      Voilá! I can see and synchronize it with Google Drive.

    • Administrator
      Linda Lawton

      A service account is its own Sudu user, you are uploading files to the service accounts Google Drive account. If you want to see them on your account. Do a patch on the files and update the permissions Grant your google drive account access to the files.

  • vishal middha

    Hello linda,
    As i am working on a project and i want to allow users to upload files from their account. i want to store that files into my google drive.
    Can you please help me with this issue?

    • Linda Lawton
      Linda Lawton Post author

      Probably not. Its probably the default size of a google drive account. Try running About about = service.About.Get().Execute(); see how much space it has. Either way you won’t be able to extend it. You can however give it access to a folder on your Google drive account then you can pay for an extension of the size of that.

        • Linda Lawton
          Linda Lawton Post author

          The other way around. Create a folder on your Google Pro account. Add the service account email address as a user, give it write access.

          The service account can now upload to that folder. However it is important to remember after you do a file.insert using the service account to do a permissions.insert on the file that you just inserted to give yourself access to the file other wise the service account will own the file.

          But yes if we create a bunch of service accounts and just share the folder with ourselves technically we would get more space for free 🙂 Just don’t delete the service account in dev console or you will loose it and there is no way of getting it back.

  • flashharry

    I am aware that it i possible to show your Google Photos as a subfolder within Google Drive.

    Can you use this technique in order to a service account access to your Google Photos using the Google Picasaweb API?

  • Gopal Biswas

    I couldn’t find the section Service Account details screen(showing CLIENT ID/ Email Address/ Certificate Fingerprints) section as mentioned in this article from my console.developers.google.com.
    Actually an error is showing for FileList filesFeed = list.Execute();
    and the error stated as “”invalid_client”, Description:”The OAuth client was invalid.”, Uri:”””.

    Currently I’m using my email id as it is for AuthenticateServiceAccount and I want to check and use the google created email id.
    One more thing please tell me the way to create a directory and give full permissions to it.
    Please help

    • Linda Lawton
      Linda Lawton Post author

      a service account isn’t you and you cant use your email address to authenticate you need to use the service account email address from google developers console.

      • pantonis

        When I try to do

        Permission permission = new Permission();
        permission.EmailAddress = “myemail@gmail.com”;
        permission.Type = “user”;
        permission.Role = “reader”;

        var cr = service.Permissions.Create(permission, obj.Id).Execute();
        var updatePermission = service.Permissions.Update(permission, obj.Id, cr.Id);
        updatePermission.TransferOwnership = true;

        I get

        The transferOwnership parameter must be enabled when the permission role is ‘owner’. [403]
        Errors [
        Message[The transferOwnership parameter must be enabled when the permission role is ‘owner’.] Location[transferOwnership – parameter] Reason[forbidden] Domain[global]

  • Walter ZAMBOTTI

    Hello Linda

    Thank you for providing these fine examples.

    In relation to the Google Drive API with a Service Account example I have successfully used it to create a drive service as expected.

    However I am unable to use the resulting drive service to do anything (list files, upload etc).

    Can you help me to understand the significance of the “ApplicationName” in relation to the drive service and what should this application refer to? i.e: Is this application something that I need to register via the google admin console?

    • Walter ZAMBOTTI

      I had a break through!

      When I was supplying the google account email I was using the email address of the associated with the actually google drive. For example

      MyGmailAccount@gmail.com : It was was under this google account that have my normal access and where I created the service account key.

      However when setting up my service account key I noticed another email address called the service account ID.

      For example:

      Service-Account-Name@logical-codex-149004.iam.gserviceaccount.com (which is auto generated by the admin console)

      When I use this email address with ServiceAccountCredential.Initializer it all works.

      I am a little confused! Any clarification appreciated.

      • Linda Lawton
        Linda Lawton Post author

        A service account isn’t you even though it was created by your gmail account in Google developer console. Think of service accounts as dummy user accounts, it has its own google drive account, google calendar, and probably a bunch more. It has access it its own account, you can share your information with it like you would any other user. (Try sharing a file with it from your drive account by adding its email address like you would any other user).

        You might want to try reading this Google Developer console service account it should help remove your confusion.

        • Walter ZAMBOTTI

          And further if you use this to initialize the credentials all files uploaded via the service account will immediately appear for the regular use of the google account without playing around with permissions.

          sacredential = new ServiceAccountCredential
          new ServiceAccountCredential.Initializer(serviceAccountEmail)
          Scopes = scopes,
          User = regularAssociatedAccountEmail // add this to allow the account user to see uploaded files


          • Linda Lawton
            Linda Lawton Post author

            That one i wasn’t aware of. Going to have to dig around in the library on that one. I though User was just for creating the credentials on the machine. Nice find.

          • Luca

            Hi. I was looking for the same functionality but I am using a v3 REST service from an Angular2 application:
            – I retrieve the access token for the service account through my server
            – I have shared a folder in my main Google Drive account (Pro version) with the service account email
            – I can use the REST calls to upload files in the folder of the Pro account
            – the owner of the files remain the service account
            – when I try to change the owner using the “permissions” REST interface, I get errors like “ACL change not allowed”. I can only create a “write” permission of the Pro account, not an “owner” permission.
            In summary what I try to achieve is to move the files from the service account to the pro account programmatically.
            I have tried to find this “regularAssociatedAccountEmail” in the documentation, but I was not able to find anything.
            Any suggestion?

          • Luca

            I found out that service accounts can be used to “impersonate” another user inside the domain:
            When getting the Auth token from my server I just have to set the email address of the user to impersonate:
            The token received allows to store files with correct owner and not service account owner.

  • Sanket

    cann’t we use Google driver api with service account for accessing other google user’s drive information ?
    we have requirement like above. we wrote code and its working for google user in which we created service account but not for other user google.
    we added other user email at where we set scope.
    we got error like “unauthorized…..
    can you please help in this case?


    • Linda Lawton
      Linda Lawton Post author

      Service accounts must be pre-authorized. Any directory you want it to be able to access must be shared with it. Have the other user take the service account email address and share a directory with it. Service accounts can only access directories of users who have shared with them.

  • Dkns

    Hi Linda,
    Is there any code or methods of google drive api so that i can iterate for each user drive by service account. We have enterprise account and generate p12 for that service account and got the list of users but stuck to access every users files and folders

    • Linda Lawton
      Linda Lawton Post author

      The service account is a user you can only access the files that the service account has access to not another users.

      I suggest you try looping though each file and see who the owner is if its not the service accounts its probably one of the other users you are speaking of.