- Home /
Accessing Google APIs in Unity
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!
Answer by kromenak · May 07, 2012 at 05:34 AM
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.
Where can I read more about how you did to access Google APIs? I'm kind of stuck with that and I'd be curious to know more on how push some stuff on Google Spreasheets. Thanks.
Google's API documentation is the best place to get info on accessing APIs - it is clearly written and pretty easy to follow. The trickiest part for me was the OAuth stuff (which was mainly what prompted this question). You can find the documentation here: https://developers.google.com/google-apps/documents-list/
@kromenak - any chance you could post something on how that OAuth worked for you?
@kromenak in fact I was wondering, as @whydoidoit asked, if you could explain how you did. Anyway it would make a very interesting and useful page to have on the wiki.
I've posted some OAuth and setup stuff as a post on my blog: http://supersegfault.com/?p=212
Hope it helps, let me know if anything needs more clarification.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071150im_/https://answers.unity.com/themes/thub/images/avi.jpg)