I’m trying to create a virtual friend in Unity for iOS, where I use the Vertex AI model Gemini Flash for communication, but I can’t just use an API key for that communication; Vertex AI need OAuth 2.0 authentication. I tried code using the Google.Apis.Auth.OAuth2 library, but it seems to not work when building for iOS. Json is loaded correctly but error:
<code>Failed to initialize Google credential: Json is Empty or null
</code>
<code>Failed to initialize Google credential: Json is Empty or null
</code>
Failed to initialize Google credential: Json is Empty or null
Is there an alternative option?
<code>private async Task InitializeCredential()
{
try
{
// Cesta ke služebnímu účtu v StreamingAssets
var resourcePath = Path.Combine(Application.streamingAssetsPath, apiKeyPath + ".json");
string jsonKey;
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
using (UnityWebRequest www = UnityWebRequest.Get(resourcePath))
{
await www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
throw new FileNotFoundException($"Failed to load service account JSON file: {www.error}");
}
jsonKey = www.downloadHandler.text;
}
}
else
{
jsonKey = File.ReadAllText(resourcePath);
}
// Vytvoření Google Credential z JSON klíče
using (var jsonKeyStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonKey)))
{
_credential = GoogleCredential.FromStream(jsonKeyStream)
.CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });
}
}
catch (Exception ex)
{
Debug.LogError($"Failed to initialize Google credentials: {ex.Message}");
}
}
</code>
<code>private async Task InitializeCredential()
{
try
{
// Cesta ke služebnímu účtu v StreamingAssets
var resourcePath = Path.Combine(Application.streamingAssetsPath, apiKeyPath + ".json");
string jsonKey;
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
using (UnityWebRequest www = UnityWebRequest.Get(resourcePath))
{
await www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
throw new FileNotFoundException($"Failed to load service account JSON file: {www.error}");
}
jsonKey = www.downloadHandler.text;
}
}
else
{
jsonKey = File.ReadAllText(resourcePath);
}
// Vytvoření Google Credential z JSON klíče
using (var jsonKeyStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonKey)))
{
_credential = GoogleCredential.FromStream(jsonKeyStream)
.CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });
}
}
catch (Exception ex)
{
Debug.LogError($"Failed to initialize Google credentials: {ex.Message}");
}
}
</code>
private async Task InitializeCredential()
{
try
{
// Cesta ke služebnímu účtu v StreamingAssets
var resourcePath = Path.Combine(Application.streamingAssetsPath, apiKeyPath + ".json");
string jsonKey;
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
using (UnityWebRequest www = UnityWebRequest.Get(resourcePath))
{
await www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
throw new FileNotFoundException($"Failed to load service account JSON file: {www.error}");
}
jsonKey = www.downloadHandler.text;
}
}
else
{
jsonKey = File.ReadAllText(resourcePath);
}
// Vytvoření Google Credential z JSON klíče
using (var jsonKeyStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonKey)))
{
_credential = GoogleCredential.FromStream(jsonKeyStream)
.CreateScoped(new[] { "https://www.googleapis.com/auth/cloud-platform" });
}
}
catch (Exception ex)
{
Debug.LogError($"Failed to initialize Google credentials: {ex.Message}");
}
}
<code>public async Task<string> GetAccessTokenAsync()
{
if (_credential == null)
{
Debug.LogError("Google Credential is not initialized.");
return null;
}
try
{
var tokenResponse = await _credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return tokenResponse;
}
catch (Exception ex)
{
Debug.LogError($"Failed to obtain access token: {ex.Message}");
throw;
}
}
</code>
<code>public async Task<string> GetAccessTokenAsync()
{
if (_credential == null)
{
Debug.LogError("Google Credential is not initialized.");
return null;
}
try
{
var tokenResponse = await _credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return tokenResponse;
}
catch (Exception ex)
{
Debug.LogError($"Failed to obtain access token: {ex.Message}");
throw;
}
}
</code>
public async Task<string> GetAccessTokenAsync()
{
if (_credential == null)
{
Debug.LogError("Google Credential is not initialized.");
return null;
}
try
{
var tokenResponse = await _credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return tokenResponse;
}
catch (Exception ex)
{
Debug.LogError($"Failed to obtain access token: {ex.Message}");
throw;
}
}
New contributor
ParagooFn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.