- Home /
GoogleApiException: Parameter "spreadsheetId" is missing
Hey everyone, I'm trying to access Google Spreadsheets API in my app. But i have ran into a problem on the il2cpp Android build of my app.
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.");
}
}
}
It's a simple script that gets the Google Sheet with the "MY_SPREADSHEET_ID", then authorises my access via "MY_SERVICE_ACCOUNT" , with the p12 key linked to the service account. The script has no problems, running on Editor. On Android i can access the p12 path and i dont think i have auth issues. But when i run it i get this exception on Android Logcat:
I have no idea what could be causing this exception since it can perfectly read and get data from the spreadsheet on Editor. I tried a const string variable, getting rid of the variable and just typing the string to the method field. I tried to search online but it seems nobody has this problem but me.
Thanks in advance.
The same issue happens on the $$anonymous$$ono build as well.
Answer by Frostoise · Oct 12, 2020 at 06:17 PM
Alright so, Google Sheets is not officially supported for Unity. You can force it with NuGetforUnity, and it works for mono pc builds, but i guess android port with il2ccp makes it not work.
If anyone encounters the same problem, i recommend using Google Firebase for data transfer, instead. It is fully supported for Unity and so far worked perfectly with all my builds.
Answer by FrostweepGames · Feb 14 at 08:18 AM
Hello,
the fix is to add a link.xml file with these content:
<linker>
<!-- Preserve types and members in an assembly -->
<assembly fullname="Google.Apis.Auth" preserve="all" />
<assembly fullname="Google.Apis.Auth.OAuth2" preserve="all" />
<assembly fullname="Google.Apis.Auth.PlatformServices" preserve="all" />
<assembly fullname="Google.Apis.Sheets.v4" preserve="all" />
<assembly fullname="Google.Apis.Core" preserve="all" />
<assembly fullname="Google.Apis" preserve="all" />
<assembly fullname="Google.Apis.PlatformServices" preserve="all" />
<assembly fullname="Newtonsoft.Json" preserve="all" />
</linker>
Your answer
Follow this Question
Related Questions
embed google maps into unity 3d 4 Answers
WWW Request for image error, unsupported URL 1 Answer
Android https request returns SSLHandshakeException 2 Answers
Exception: not implemented? 2 Answers
How to write a HTTPS Post Request 1 Answer