簡體   English   中英

反序列化 JSON 憑據數據時出錯。 谷歌雲文件上傳 C# Asp.Net

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

我正在嘗試建立與谷歌雲的連接以進行媒體上傳

我使用了各種方法,例如設置環境變量,將 json 保存到 mongodb ,嘗試直接從文件加載 json 然后加載到 google 函數

圖書館:

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;  

方法一:使用環境變量。

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.  

請檢查環境變量 GOOGLE_APPLICATION_CREDENTIALS 的值

方法二:從文件中加載json

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

Error deserializing JSON credential data.

方法三:從Mongodb數據庫中加載數據,然后嘗試轉成字符串

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

在這種情況下,它會給出錯誤

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

即使我已經在線檢查了解決方案
https://github.com/google/google-api-dotnet-client/issues/747

但它沒有用。 我在這里做錯了什么?

我使用方法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;

我的情況可能無法直接回答問題,但可能是導致此類錯誤的原因。 在我的項目中,我引用了兩個不同版本的 Newtonsoft.json,一個是 12.0.2,另一個是 7.0.1。 一旦我將 7.0.1 更新為相同的 12.0.2,json 反序列化錯誤就解決了。 這可能解釋了一些隨機工作/不工作的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM