1

I am trying to simply authenticate to the Google Cloud Storage API using OAuth2.0 and C# with a JSON file from Google.

here is my OAuth JSON file:

{
    "client_id": "ClientIdHere",
    "project_id": "ProjectName",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "ClientSecretHere",
    "redirect_uris": [ "https://localhost:44349//RedirectPage.aspx" ]
  }

Here is the code that I am using, I borrowed it from this post

       protected void DoTheThingsAgain()
        {
            try
            {
                string bucketName = "Daves-Bucket";
                string sharedkeyFilePath = "C:\\inetpub\\wwwroot\\GoogleAPITest\\GoogleAPITest\\App_Data\\JSONFile.json";
                GoogleCredential credential = null;

                using (var jsonStream = new FileStream(sharedkeyFilePath, FileMode.Open,
                    FileAccess.Read, FileShare.Read))
                {
                    credential = GoogleCredential.FromStream(jsonStream);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Everything works fine until it hits this line:

**credential = GoogleCredential.FromStream(jsonStream);**

then it gives me the error: Error creating credential from JSON. Unrecognized credential type .

How do I specify the credential type for OAuth?

I have seen other solutions for this but not while using OAuth, only with ServiceAccounts.

That you in advance for any help you all can provide!

dspyank
  • 127
  • 1
  • 2
  • 12
  • `GoogleCredential.FromStream()` seems to call [`DefaultCredentialProvider.CreateDefaultCredentialFromStream(Stream stream)`](https://github.com/googleapis/google-api-dotnet-client/blob/master/Src/Support/Google.Apis.Auth/OAuth2/DefaultCredentialProvider.cs#L159) which tries to deserialize a [`JsonCredentialParameters`](https://github.com/googleapis/google-api-dotnet-client/blob/master/Src/Support/Google.Apis.Auth/OAuth2/JsonCredentialParameters.cs). – dbc Jul 21 '21 at 15:32
  • `JsonCredentialParameters` corresponds to the ***contents*** of the `"web"` property in your JSON. Why do you have the `{"web":{ ... }}` wrapper in your JSON? Can you avoid storing it, or strip it off? – dbc Jul 21 '21 at 15:33
  • @dbc that was included as part of the JSON file that I downloaded from the page for OAuth on the Google API Credentials Dashboard, I will try removing it and see if that helps. – dspyank Jul 21 '21 at 15:40
  • @dbc I tried removing the web wrapper. I got the same type error. – dspyank Jul 21 '21 at 15:43
  • 2
    Hmmm, it seems your JSON is missing the [`"type"`](https://github.com/googleapis/google-api-dotnet-client/blob/master/Src/Support/Google.Apis.Auth/OAuth2/JsonCredentialParameters.cs#L48) property: *Type of the credential.*. Maybe that's the problem? – dbc Jul 21 '21 at 15:45
  • @dbc that solved my problem! I couldn't find any types besides "service_account", Ive tried OAuth, OAuth2.0, but then I looked in the link you sent and saw: type: "authorized_user" Thank you so much! – dspyank Jul 21 '21 at 15:53

0 Answers0