Hey everyone,
I’m trying to send and receive data from Google’s APIs in the Unity editor so, for example, I can enter data into a Google Docs spreadsheet and grab the data to display in the editor.
Google supports this through .NET, which should have made this a walk in the park. However, when I run my code to send requests to Google, they fail with a couple error variations:
WebException: Error writing request: The authentication or decryption has failed.
TLSException
Has anyone been able to get this sort of thing to work? Based on research into the problem so far, it seems that Mono does not like to do HTTPS requests to sources it doesn’t trust (and it trusts NO sources by default). There are instructions for adding trusted sources to Mono, but I can’t get them to work with Unity’s version of Mono for some reason.
And unfortunately, I can’t use the WWW class (which is supposed to support HTTPS) because these HTTPS requests are occurring inside of Google’s DLL files.
Any thoughts? I may have to turn to SQL in the end, but Google provides such a nice editing interface, it would save me a lot of time. Thanks!
Got this working after a long and bloody battle, here is how I did it!
One option for handling this particular security issue is to override the certificate policy in Mono with your own policy. Note that this is a somewhat incomplete solution because it is effectively bypassing certificate security - something NOT to be done in production, published code.
My current solution is to override the certificate policy with an extremely dumb policy that just says “Yeah, ok, you can connect”. In the future, I could make this check the certificate and either accept or reject based on where the certificate is from. Here is the code:
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class UnsafeSecurityPolicy {
public static bool Validator(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors policyErrors) {
//*** Just accept and move on...
Debug.Log ("Validation successful!");
return true;
}
public static void Instate() {
ServicePointManager.ServerCertificateValidationCallback = Validator;
}
}
You just need to call “Instate()” somewhere in your code before you make requests to HTTPS. This should avoid the errors I mentioned in my original question, and I was able to successfully connect to Google Docs and view some spreadsheet data.