Leave a comment

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

42 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]