Search code examples
c#visual-studio-2015google-sheets-apix509certificate2notimplementedexception

NotImplementedException when Accessing ServiceAccountCredential C#


I have a Windows app that access a Google spreadsheet to create a phone book. When I try to open the phone book, I get flagged with System.NotImplementedException: The method or operation is not implemented. I'm not really sure why, because it seems like it is being implemented?

This is the first spot that's being flagged with the issue:

internal object FromCertificate(X509Certificate2 certificate)
        {
            throw new NotImplementedException(); //Error flags this line
        }

This is the second. As far as I can tell, there's nothing wrong with this part but it's still flagging an exception.

ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               Scopes = new[] { "https://www.googleapis.com/auth/spreadsheets", "https://docs.google.com/feeds" }
           }.FromCertificate(certificate));

Any suggestions would be really appreciated. I'm using C# with Visual Studio 2015.


Solution

  • I'm not really sure why, becayse it seems like it is being implemented?

    The giveaway is this line here:

    throw new NotImplementedException();
    

    The throw new NotImplementedException() is usually boiler-plate from Visual Studio when stubbing out methods.

    It isn't being implemented, just because you're calling it from the object with .FromCertificate(certificate). This is just calling the method, which then runs the code within it. Ergo, it then hits your throw new NotImplementedException(); code - thus breaking etc.

    To implement your method, you need to remove the throw new NotImplementedException(); and replace it with your own logic.

    You will need to add some code in your FromCertificate() method:

    internal object FromCertificate(X509Certificate2 certificate)
    {
        // Do some work in here
    }
    

    Hope this helps :)