JsonSerializationException'无法找到构造函数'为GoogleApi

问题描述

我目前正在使用Unity构建iOS应用。

我一直在使用dll插件Google.Apis.Auth,Google.Apis.Core,Google.Apis.Sheets.v4,Newtonsoft.Json,以便在应用程序启动时从Google工作表中读取数据。 我已经在Windows和iOS上完成了构建。 Windows One完美运行,当应用程序打开时,它将自动打开Chrome,并要求登录以访问Google工作表。然后您返回该应用程序,它将加载数据。

但是,对于iOS版本,我遇到了一个问题。每当我在XCode中构建并运行iOS构建并将其运行到iPhone时,都会出现错误

JsonSerializationException Unable to find a constructor for type Google.Apis.Auth.Oauth2.GoogleClientSecret 
A class should either have a default constructor,one constructor with arguments or a constructor marked with the JsonConstructor attribute.

到目前为止我已经尝试过:

  • 在GoogleClientSecret类的构造函数上方添加 [JsonConstructor]
  • 创建一个link.xml文件
  • 将凭据.json文件放入流媒体资源中。
  • 使用google-api-dotnet-client的源代码

如果有任何代码示例或其他信息有帮助,我非常乐意提供。对于解决此问题的任何提示将不胜感激。

以下是用于阅读Google工作表的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Util.Store;
using UnityEngine;
using Object = System.Object;

namespace GoogleSheetsLevelSynchronizer
{
    static class GoogleSheetReader
    {
        // If modifying these scopes,delete your prevIoUsly saved credentials
        // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
        static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
        static string ApplicationName = "google-sheets-api-Quickstart";

        public static IList<IList<object>> Reader(String spreadsheetId,String range)
        {
            UserCredential credential;

            using (var stream =
                new FileStream(Application.streamingAssetsPath + "/credentials.json",FileMode.Open,FileAccess.Read))
            {
                // The file token.json stores the user's access and refresh tokens,and is created
                // automatically when the authorization flow completes for the first time.
                string credpath = "token.json";
                credential = GoogleWebAuthorizationbroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,Scopes,"user",CancellationToken.None,new FileDataStore(credpath,true)).Result;
                Debug.Log("Credential file saved to: " + credpath);
            }

            // Create Google Sheets API service.
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientinitializer = credential,ApplicationName = ApplicationName,});

            // Define request parameters.
            
            SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId,range);

            // Prints the names and majors of students in a sample spreadsheet:
            // https://docs.google.com/spreadsheets/d/[spreadsheetId]/edit
            ValueRange response = request.Execute();
            IList<IList<object>> values = response.Values;
            
            if (values != null && values.Count > 0)
            {
                Debug.Log("Data Found!! :)");
            }
            else
            {
                Debug.LogError("No data found.");
            }
            // Console.Read();

            return values;
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)