Aquafreax
Aquafreax

Reputation: 316

Error deserializing JSON credential data. Google Cloud File Upload C# Asp.Net

I'm trying to establish connection to google cloud for media upload

I've used various methods like Setting environment variable, saving json to mongodb , tried directly load json from file and then loading to google functions

libraries:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using DAL;  
using MongoDB;  
using System.Data;  
using System.Data.OleDb;  
using System.IO;  
using System.Configuration;  
using Google.Apis.Auth.OAuth2;  
using Google.Apis.Auth.OAuth2.Flows;  
using System.Drawing;
using Newtonsoft.Json;
using MongoDB;
using MongoDB.Driver.Builders;
using MongoDB.Bson;  

Method 1: Using Environment Variable.

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS","onfer-gc.json");  
var Credential1 = GoogleCredential.GetApplicationDefaultAsync();  

Error : Using Environment Variable.
Error reading credential file from location onfer-gc.json: Error deserializing JSON credential data.  

Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS

Method 2: Load json from file

FileStream stream1 = File.OpenRead("onfer-gc.json");
var Credential = GoogleCredential.FromStream(stream1);

Error deserializing JSON credential data.

Method 3: Loading data from Mongodb database and then trying to turn into string

var Db = new Database();
var result = Db.GetCollection<company>().FindOne(Query.EQ("type", "service_account"));
var Credential = GoogleCredential.FromJson(result);

In this case it gives error

Element 'type' does not match any field or property of class MongoDB.company.

Even I've checked solutions online
https://github.com/google/google-api-dotnet-client/issues/747

But it didn't work. What am i doing wrong here ?

Upvotes: 4

Views: 4309

Answers (2)

Luke Yang
Luke Yang

Reputation: 31

My case may not answer the question directly, but it might be possible cause for such error. In my projects I have references to two different versions of Newtonsoft.json, one is 12.0.2 and the other one is 7.0.1. Once I had 7.0.1 updated to same 12.0.2, the json deserializing error solved. This might explain some random working/not-working situations.

Upvotes: 3

Aquafreax
Aquafreax

Reputation: 316

I was successfull establishing connection using Method 2:

string credPath = "D:\\Admin\\gcloudcred.json";
var credential = GoogleCredential.FromStream(File.OpenRead(credPath));
var storageClient = StorageClient.Create(credential);
listvideos = storageClient.ListObjects("bucketname");
if (listvideos == null)
{
  return listvideos = listvideos1;
}
return listvideos;

Upvotes: 2

Related Questions