1

I am facing the following problem: I have created a google service account and now I am trying make an authorized API call using c# to Google API and YouTube Data API. Here is my sample code:

string filePath = @"~\my-path\Default-GASvcAcct-508d097b0bff.json"

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    googleCredential = GoogleCredential.FromStream(stream);
}

if (googleCredential.IsCreateScopedRequired)
{
    googleCredential.CreateScoped(
        new string[] 
        {
            YouTubeService.Scope.YoutubeReadonly,
            YouTubeService.Scope.YoutubeUpload,
            YouTubeService.Scope.Youtube,
            YouTubeService.Scope.YoutubeForceSsl,
            YouTubeService.Scope.Youtubepartner,
            YouTubeService.Scope.YoutubepartnerChannelAudit
        });
}

YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = googleCredential,
});

service.Videos.List()

The code builds successfully but when I start the program, I get the following error message:

System.IO.FileNotFoundException: 'Could not load file or assembly 'BouncyCastle.Crypto, Version=1.7.4137.9688, Culture=neutral, PublicKeyToken=a4292a325f69b123' or one of its dependencies. The system cannot find the file specified.'

Does somebody know how to fix this error and also, is it possible to make request to YouTube Data API with service account credentials?

Thanks in advance!

CSDev
  • 3,177
  • 6
  • 19
  • 37
Rosen Petrov
  • 188
  • 2
  • 12
  • What version of .NET do you use? – baruchiro Jun 24 '19 at 16:35
  • And which version of the NuGet packages are you using? Note that you're ignoring the return value of `CreateScoped`, too - you should have `googleCredential = googleCredential.CreateScoped(...)` – Jon Skeet Jun 24 '19 at 16:42
  • check this it might be help you https://stackoverflow.com/questions/21132531/list-youtube-videos-using-c-sharp-and-google-apis-youtube-v3 – Mangesh Auti Jun 24 '19 at 16:48
  • @Baruch in this project I am using .NET 4.5 - do you think the version of .NET could be the reason for this error? The exception has been thrown in the using statement. – Rosen Petrov Jun 25 '19 at 07:46
  • @MangeshAuti the problem is not implementing the listing videos functionality but the authorization at all. – Rosen Petrov Jun 25 '19 at 07:48
  • @RosenPetrov I am not saying it is a similar question, just check how they created user credentials of Google to access youtube – Mangesh Auti Jun 25 '19 at 08:06
  • @RosenPetrov Please edit your question to include the information about `.NET 4.5` – baruchiro Jun 25 '19 at 08:23
  • @JonSkeet After updating Google.Apis.Auth and Google.Apis.YouTube.v3 NuGet packages and changing the code as you suggested it worked. Thanks! But now I get the following error: Google.GoogleApiException: 'Google.Apis.Requests.RequestError Access Not Configured. YouTube Data API has not been used in project XXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/youtube.googleapis.com/overview?project=XXX then retry... – Rosen Petrov Jun 25 '19 at 08:29
  • And did you then follow the advice in the error message, making sure you enabled it for the exact project specified in the message? – Jon Skeet Jun 25 '19 at 08:49

1 Answers1

0

Update:

According to Jhon Skeet's comment, if you update the Google API nuget's you will no longer be dependent on BouncyCastle.Crypto nuget, so this is also an option.

Original answer:

I can not prove unequivocally, but it seems that nuget BouncyCastle.Crypto is not suitable for .NET 4.0.

You can see that there is a nuget trying to make the portability.

And I also think there is a pre-release version that should be compatible with .NET 4.0:

Nuget Package browser

baruchiro
  • 5,088
  • 5
  • 44
  • 66
  • 3
    We stopped using BouncyCastle in the Google API libraries in December 2016. The best solution here is to use a recent version, not to try to get BouncyCastle working. – Jon Skeet Jun 25 '19 at 08:52