Search code examples
c#oauth-2.0google-oauth

Google Oauth error: At least one client secrets (Installed or Web) should be set


I'm using Google's Oauth 2.0 to upload videos to Youtube via our server. My client ID is a "service account". I downloaded the json key and added it to my solution.

Here is the relevant code:

 private async Task Run(string filePath)
        {
            UserCredential credential;
            var keyUrl = System.Web.HttpContext.Current.Server.MapPath("~/content/oauth_key.json");
            using (var stream = new FileStream(keyUrl, FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows an application to upload files to the
                    // authenticated user's YouTube channel, but doesn't allow other types of access.
                    new[] { YouTubeService.Scope.YoutubeUpload },
                    "user",
                    CancellationToken.None
                );
            }

            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

When I run it, I get this error: At least one client secrets (Installed or Web) should be set.

However, in my json there is no "client secret":

{
  "private_key_id": "9d98c06b3e730070806dcf8227578efd0ba9989b",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIICdQIBADANBgkqhk etc,
  "client_email": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04@developer.gserviceaccount.com",
  "client_id": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04.apps.googleusercontent.com",
  "type": "service_account"
}

so I assume I overlooked something. Maybe I can't use the "service account" ? don't know...


Solution

  • Not an expert on C# but it looks like you were trying to use the service account to do the OAuth2 web server flow, which shouldn't work. You probably want to use ServiceAccountCredential instead. For more information about different Google OAuth2 flows, please refer to the doc for web server, service account, etc.