Unity3d ';GoogleApiException:参数“0”;电子表格;缺少';尝试访问Google Sheets API时出现异常

Unity3d ';GoogleApiException:参数“0”;电子表格;缺少';尝试访问Google Sheets API时出现异常,unity3d,google-api,google-sheets-api,Unity3d,Google Api,Google Sheets Api,我正在尝试使用以下脚本访问Unity上的Google Sheets API: using System.Collections.Generic; using UnityEngine; using Google.Apis.Auth.OAuth2; using Google.Apis.Sheets.v4; using Google.Apis.Services; using Google.Apis.Sheets.v4.Data; using System.Security.Cryptography.

我正在尝试使用以下脚本访问Unity上的Google Sheets API:

using System.Collections.Generic;
using UnityEngine;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Services;
using Google.Apis.Sheets.v4.Data;
using System.Security.Cryptography.X509Certificates;

using System.Text;
using TMPro;
public class GoogleSheetsTest : MonoBehaviour
{

 public TextMeshProUGUI text;

 string p12PathFromAsset;

 
 const string sheetNameAndRange = "A!A1:D13";
 const string serviceAccountEmail = "MY_SERVICE_ACCOUNT";
 static SheetsService service;

 private void Awake()
 {
     RequestPath();
 }

 void RequestPath()
 {
     string keyPath = "P12_KEY_LOCATION";
     string realPath;

     if (Application.platform == RuntimePlatform.Android)
     {
         // Android
         string oriPath = System.IO.Path.Combine(Application.streamingAssetsPath, keyPath);

         // Android only use WWW to read file
         WWW reader = new WWW(oriPath);
         while (!reader.isDone) { }

         realPath = Application.persistentDataPath + "P12_KEY_STUFF";
         System.IO.File.WriteAllBytes(realPath, reader.bytes);

         Debug.Log("Running Android");

         p12PathFromAsset = realPath.Replace("p12",".p12");
     }
     else
     {
         // iOS
         p12PathFromAsset = System.IO.Path.Combine(Application.streamingAssetsPath, keyPath);
     }

     SyncData();

 }

 void SyncData()
 {

     var certificate = new X509Certificate2(p12PathFromAsset, "notasecret", X509KeyStorageFlags.Exportable);

     text.text = p12PathFromAsset;

     ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes = new[] { SheetsService.Scope.Spreadsheets }
            /*
             Without this scope, it will :
             GoogleApiException: Google.Apis.Requests.RequestError
             Request had invalid authentication credentials. Expected OAuth 
             2 access token, login cookie or other valid authentication 
             credential.
             lol..
             */
        }.FromCertificate(certificate));

     service = new SheetsService(new BaseClientService.Initializer()
     {
         HttpClientInitializer = credential,
     });

     string spreadsheetid = "MY_SPREADSHEET_ID";
     var request = service.Spreadsheets.Values.Get(spreadsheetid, sheetNameAndRange);

     StringBuilder sb = new StringBuilder();

     ValueRange response = request.Execute();
     IList<IList<object>> values = response.Values;
     if (values != null && values.Count > 0)
     {
         foreach (IList<object> row in values)
         {
             foreach (object cell in row)
             {
                 sb.Append(cell.ToString() + " ");
             }

             //Concat the whole row
             Debug.Log(sb.ToString());
             text.text = sb.ToString();

             sb.Clear();
         }
     }
     else
     {
         Debug.Log("No data found.");
     }
 }
    
使用System.Collections.Generic;
使用UnityEngine;
使用Google.api.Auth.OAuth2;
使用Google.api.Sheets.v4;
使用Google.api.Services;
使用Google.api.Sheets.v4.Data;
使用System.Security.Cryptography.X509证书;
使用系统文本;
使用TMPro;
公共类Google Sheetstest:单一行为
{
公共文本;
字符串p12PathFromAsset;
常量字符串SheetName和Range=“A!A1:D13”;
const string serviceAccountEmail=“我的服务账户”;
静态纸张服务;
私人空间
{
请求路径();
}
void RequestPath()
{
字符串keyPath=“P12\u键位置”;
字符串实路径;
if(Application.platform==RuntimePlatform.Android)
{
//安卓
字符串oriPath=System.IO.Path.Combine(Application.streamingassetpath,keyPath);
//Android仅使用WWW读取文件
WWW阅读器=新WWW(oriPath);
而(!reader.isDone){}
realPath=Application.persistentDataPath+“P12\u KEY\u STUFF”;
System.IO.File.writealBytes(realPath,reader.bytes);
Log(“运行Android”);
p12PathFromAsset=realPath.Replace(“p12”和“.p12”);
}
其他的
{
//iOS
p12PathFromAsset=System.IO.Path.Combine(Application.StreamingAssetPath,keyPath);
}
SyncData();
}
void SyncData()
{
var证书=新的X509Certificate2(p12PathFromAsset,“notasecret”,X509KeyStrageFlags.Exportable);
text.text=p12PathFromAsset;
ServiceAccountCredential credential=新ServiceAccountCredential(
新ServiceAccountCredential.初始值设定项(serviceAccountEmail)
{
Scopes=new[]{SheetsService.Scope.Spreadsheets}
/*
如果没有这个范围,它将:
GoogleApiException:Google.api.Requests.RequestError
请求具有无效的身份验证凭据。应为OAuth
2访问令牌、登录cookie或其他有效身份验证
资质
英雄联盟
*/
}.FromCertificate(证书));
服务=新的SheetsService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
});
string spreadsheetid=“我的电子表格ID”;
var请求=service.Spreadsheets.Values.Get(电子表格ID、sheetNameAndRange);
StringBuilder sb=新的StringBuilder();
ValueRange响应=请求。执行();
IList值=响应值;
if(值!=null&&values.Count>0)
{
foreach(值中的IList行)
{
foreach(行中的对象单元格)
{
sb.Append(cell.ToString()+);
}
//整排
Log(sb.ToString());
text.text=sb.ToString();
(某人清楚地);
}
}
其他的
{
Log(“找不到数据”);
}
}
这是一个相对简单的脚本,它使用spreadsheetId:MY_SPREADSHEET_ID获取google工作表,并使用p12键通过google服务帐户授权访问。“a!A1:D13”是它查找的电子表格的行和列

这在Unity Editor上运行良好。它可以在android上获取p12键位置并验证访问权限,但它会出现此异常,无法检索电子表格数据:

2020.10.09 16:42:06.910 6047 6067错误:参数“spreadsheetId”缺失

2020.10.09 16:42:06.910 6047 6067 Google.API.Requests.ClientServiceRequest`1[TreResponse].AddParameters(Google.API.Requests.RequestBuilder RequestBuilder,Google.API.Requests.Parameters.ParameterCollection inputParameters)[0x00000]中出现错误:0

2020.10.09 16:42:06.910 6047 6067 Google.API.Requests.ClientServiceRequest
1[TreResponse].CreateBuilder()[0x00000]中的错误统一:020.10.09 16:42:06.910 6047 6067 Google.API.Requests.ClientServiceRequest
1[TreResponse].CreateRequest(System.Nullable
1[T]overrideGZipEnabled)[0x00000]in:0 2020.10.09 16:42:06.910 6047 6067 DG.Tweening.TweenCallback处的错误单位
1[T].EndInvoke(System.IAsyncResult结果)[0x00000]in:0

2020.10.09 16:42:06.910 6047 6067 System.Net.Http.Headers.HttpHeaders.SetValue[T](System.String名称,T值,System.Func`2[T,TResult]ToString Converter)[0x00000]处的错误单位:0

2020.10.09 16:42:06.910 6047 6067 Google.API.Requests.ClientServiceRequest`1[TreResponse].ExecuteUnparsedAsync(System.Threading.CancellationToken ca

我已经为电子表格id尝试了一个const string变量,去掉了该变量,只在方法上键入字符串,但我认为问题不在于id,因为该脚本在UnityEditor上工作得非常好


我对此完全不知所措,非常感谢您的帮助。提前谢谢。

事实证明,Google Sheets不支持Unity。您可以使用NuGetforUnity附加组件强制它,它可以与PC Mono端口一起正常工作,但api可以在不同的端口上中断

我只是简单地放弃了工作表,我使用GoogleFirebase进行数据传输。它完全支持Unity,到目前为止运行良好