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.