'GoogleApiException: Parameter "spreadsheetId" is missing' 尝试访问 Google 工作表时出现异常 API

'GoogleApiException: Parameter "spreadsheetId" is missing' exception trying to access 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.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.");
     }
 }
    

这是一个相对简单的脚本,它获取 google Sheet 和 spreadsheetId:MY_SPREADSHEET_ID,并使用 p12 密钥通过 Google 服务帐户授权访问. “A!A1:D13”是它要查找的电子表格的行和列。

这在 Unity 上工作正常 Editor.It 设法在 android 上获取 p12 密钥位置并授权访问,但它给出了这个异常并且无法检索电子表格数据:

2020.10.09 16:42:06.910 6047 6067 Error Unity GoogleApiException: Parameter "spreadsheetId" is missing

2020.10.09 16:42:06.910 6047 6067 Error Unity at Google.Apis.Requests.ClientServiceRequest`1[TResponse].AddParameters (Google.Apis.Requests.RequestBuilder requestBuilder, Google.Apis.Requests.Parameters.ParameterCollection inputParameters) [0x00000] in <00000000000000000000000000000000>:0

2020.10.09 16:42:06.910 6047 6067 Error Unity at Google.Apis.Requests.ClientServiceRequest1[TResponse].CreateBuilder () [0x00000] in <00000000000000000000000000000000>:0 2020.10.09 16:42:06.910 6047 6067 Error Unity at Google.Apis.Requests.ClientServiceRequest1[TResponse].CreateRequest (System.Nullable1[T] overrideGZipEnabled) [0x00000] in <00000000000000000000000000000000>:0 2020.10.09 16:42:06.910 6047 6067 Error Unity at DG.Tweening.TweenCallback1[T].EndInvoke (System.IAsyncResult result) [0x00000] in <00000000000000000000000000000000>:0

2020.10.09 16:42:06.910 6047 6067 Error Unity at System.Net.Http.Headers.HttpHeaders.SetValue[T] (System.String name, T value, System.Func`2[T,TResult] toStringConverter) [0x00000] in <00000000000000000000000000000000>:0

2020.10.09 16:42:06.910 6047 6067 Error Unity at Google.Apis.Requests.ClientServiceRequest`1[TResponse].ExecuteUnparsedAsync (System.Threading.CancellationToken ca

我已经为电子表格 ID 尝试了一个 const 字符串变量,摆脱了变量,只是在方法上键入字符串,但我认为问题不在于 id,因为该脚本在 UnityEditor 上运行完美。

我对此完全不知所措,感谢任何帮助。提前致谢。

事实证明 Google Unity 不正式支持 Sheets。您可以使用 NuGetforUnity add-on 强制执行它,它可以在 PC Mono 端口上正常工作,但 api 可能会在不同的端口上中断。

我只是放弃了表格,我正在使用 Google Firebase 进行数据传输。它完全支持 Unity,目前运行良好。