Google Drive SDK for Unity (free, open-sourced)

Project on GitHub: github.com/Elringus/UnityGoogleDrive
Any questions and/or suggestions are welcome in this thread or via the GitHub issues.

Google Drive API library for listing, searching, creating, uploading, editing, copying, downloading, deleting and exporting files on the user’s drive from within Unity game engine.

Works with Unity version 5.6 and higher. Supports all the major platforms: Windows, Mac, Linux, iOS, Android and WebGL.

The design mostly follows the official Google APIs Client Library for .NET:

// Listing files.
GoogleDriveFiles.List().Send().OnDone += fileList => ...;

// Uploading a file.
var file = new UnityGoogleDrive.Data.File() { Name = "Image.png", Content = rawImageData };
GoogleDriveFiles.Create(file).Send();

// Downloading a file.
GoogleDriveFiles.Download(fileId).Send().OnDone += file => ...;

// All the requests are compatible with the .NET 4 asynchronous model.
var aboutData = await GoogleDriveAbout.Get().Send();

Three authentication schemes are used: browser redirection for WebGL builds, custom URI for iOS/Android and local loopback requests for other platforms. All the credentials are stored in a scriptable object; editor script provides shortcuts to create and manage Google Console App, allows to parse credentials JSON to skip manual copy-pasting and edit common settings:

Automated integration tests cover the main features:

The project is open-sourced and I’m planning to support it until Google will make an official Drive API library for Unity or it’ll become possible to use the current official .NET API library in Unity seamlessly on all the platforms.

7 Likes

Taking advantage of this thread, I would like to ask for an advice regarding JSON handling. Currently I’m forced to use a third-party library (Json.Net.Unity3D) to de-/serialize JSON data, as Unity’s built-in one doesn’t suport nullable types (and they’re heavily used in the data API to represent properties not included to a request/response). While the library itself works fine and is .NET 2.0 compatible, the performance is not that good comparing to the native Unity solution and I was really up to making a ‘pure’ solution, without any third-party dependencies. Maybe someone here has an idea how to make nullable types work with the Unity’s JSON tools without complicating code base too much?


I’ve created a feedback request to add support for nullable types to JsonUtility: https://feedback.unity3d.com/suggestions/add-support-for-nullable-types-to-jsonutility

Will really appreciate any votes you can spend on this idea :slight_smile:

1 Like

wish u good luck ELRINGUS …
i tried it … it works fine

1 Like

hmmm
what about if i wanna upload SQLite File ?

It doesn’t really matter what type of file to upload, you just send the binary content and (optionally) specify mime type.

1 Like

i really love to make that … can u explain more …

i think i need to change that
public Texture2D ImageToUpload;

if you can help me with that …i am new in programming i know maybe 10% lol

1 Like

You can omit mime type and use any name you like. What really matters is the binary data you’re uploading (‘Content’ property); you’ll have to get the content of the file (as byte array) you wish to upload and use it. If the file is stored locally, you can probably use File.ReadAllBytes method.

1 Like

ok thanks i will try to do that i hope i can …
i did not understand anything but i will read about it
and come back to u soon …

Hi Elringus

Thank you so much for the sdk! I only have one question: You say it works for all major Plattforms, so does that include Android and iOS too?

Cheers,
Daniel

Yep, it should work on those platforms. Personally, I’ve only tested it on PC, Android and WebGL, but it should also work on iOS. If you find any issues, please report them here or via GitHub and I’ll take a look.

1 Like

Nice, thanks again! I will give it a try soon and let you know if I also find anything useful regarding the JSON handling issue :wink:

1 Like

Hi, I have big trouble to make this working on new iOS devices. I don’t get the access tokens when accepting the browser. I switch back to the app, but nothing happens.
But this only encounters when using wifi. When I disable it and use mobile internet I don’t have these troubles.

Running current Xcode beta 4 and unity 2017.3.1 p3

2018-03-15 22:48:06.105395+0100 artemplategd[1251:354052] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-03-15 22:48:06.107633+0100 artemplategd[1251:354052] [MC] Reading from public effective user settings.
2018-03-15 22:48:12.169209+0100 artemplategd[1251:354244] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-03-15 22:48:12.169375+0100 artemplategd[1251:354244] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
-> applicationWillResignActive()
ar_session_interrupted
UnityEngine.XR.iOS.UnityARSessionNativeInterface:_ar_session_interrupted()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

-> applicationDidEnterBackground()
2018-03-15 22:48:19.235886+0100 artemplategd[1251:354244] TIC Read Status [4:0x1c0173500]: 1:57
2018-03-15 22:48:19.237008+0100 artemplategd[1251:354244] Task <1D325C96-96E3-49E0-BE05-5B93FA65A352>.<0> HTTP load failed (error code: -1005 [1:57])
2018-03-15 22:48:19.237605+0100 artemplategd[1251:354434] NSURLConnection finished with error - code -1005
-> applicationWillEnterForeground()
2018-03-15 22:48:21.366019+0100 artemplategd[1251:354434] TIC Read Status [5:0x1cc173440]: 1:57
2018-03-15 22:48:21.371482+0100 artemplategd[1251:354434] Task <E63E62CB-1CB6-4B58-B1A7-D5EF94964323>.<0> HTTP load failed (error code: -1005 [4:-4])
2018-03-15 22:48:21.382858+0100 artemplategd[1251:354751] NSURLConnection finished with error - code -1005

Thank you for reporting this! Can you please clarify whether this happens only on specific iOS devices (you’ve mentioned the new ones, but which exactly)?

iPhone X (running 11.3 15E5167f) is the trouble maker. The one that worked was an iPhone 7 (running 11.3 15E5211a) .

And an iPad with version 11.3 15E5189f had the same problem

I see, thank you for the clarification! Unfortunately, I don’t have those devices and can’t check this myself. I have a guess, that the issue could be with resolving the ‘localhost’ alias. Can you please check one more thing? Try to change the loopback uri (https://github.com/Elringus/UnityGoogleDrive/blob/master/Assets/UnityGoogleDrive/Runtime/Authorization/LoopbackAccessTokenProvider.cs#L19) to “http://127.0.0.1” and also add this address to the “Authorised redirect URIs” on the Google APIs portal (where you’ve added “http://localhost”) and try to login one more time.

Testing now.

Besides this maybe this hast to be added to info.plist?

NSAppTransportSecurity

NSAllowsLocalNetworking

Yeah, you could try adding those records. The app will definitely need local networking access. Not sure about the NSAppTransportSecurity though. Still, I’m not exactly an expert with those settings :slight_smile:

Not sure if I am doing something wrong. No luck yet. Added both things to the build.

2018-03-16 00:17:19.319998+0100 artemplategd[1342:378202] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2018-03-16 00:17:19.320733+0100 artemplategd[1342:378202] [MC] Reading from public effective user settings.
2018-03-16 00:17:25.153131+0100 artemplategd[1342:378283] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-03-16 00:17:25.153275+0100 artemplategd[1342:378283] [BoringSSL] Function boringssl_session_errorlog: line 2881 [boringssl_session_read] SSL_ERROR_ZERO_RETURN(6): operation failed because the connection was cleanly shut down with a close_notify alert
2018-03-16 00:17:32.995809+0100 artemplategd[1342:378295] XPC connection interrupted
-> applicationWillResignActive()
ar_session_interrupted
UnityEngine.XR.iOS.UnityARSessionNativeInterface:_ar_session_interrupted()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

-> applicationDidEnterBackground()
2018-03-16 00:17:55.973945+0100 artemplategd[1342:378295] dnssd_clientstub read_all(27) DEFUNCT
-> applicationWillEnterForeground()
2018-03-16 00:17:55.984349+0100 artemplategd[1342:378481] TIC Read Status [3:0x1c81684c0]: 1:57
2018-03-16 00:17:55.986453+0100 artemplategd[1342:378481] Task <4CF24BB4-7B1B-4FF8-94F7-1BD14207B0C7>.<0> HTTP load failed (error code: -1005 [1:57])
2018-03-16 00:17:55.987807+0100 artemplategd[1342:378481] TIC TCP Conn Failed [4:0x1cc1681c0]: 1:50 Err(50)
2018-03-16 00:17:55.990298+0100 artemplategd[1342:378481] Task <81839207-49D2-4B1B-963C-B6562CD704C2>.<0> HTTP load failed (error code: -1009 [1:50])
2018-03-16 00:17:55.990500+0100 artemplategd[1342:378481] TIC TCP Conn Failed [5:0x1d416fc00]: 12:8 Err(-65554)
2018-03-16 00:17:55.994681+0100 artemplategd[1342:379024] Task <22E7CC71-0C04-411A-9BAF-BFA9313E2BAC>.<0> HTTP load failed (error code: -1003 [12:8])
2018-03-16 00:17:56.003799+0100 artemplategd[1342:378988] NSURLConnection finished with error - code -1005
2018-03-16 00:17:56.007055+0100 artemplategd[1342:378988] NSURLConnection finished with error - code -1009
2018-03-16 00:17:56.007102+0100 artemplategd[1342:378988] NSURLConnection finished with error - code -1003
ar_session_interruption_ended
UnityEngine.XR.iOS.UnityARSessionNativeInterface:_ar_session_interruption_ended()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

-> applicationDidBecomeActive()
UnityGoogleDrive: Failed to exchange authorization code.
UnityGoogleDrive.LoopbackAccessTokenProvider:HandleAuthCodeExchanged(AuthCodeExchanger)
UnityEngine.AsyncOperation:InvokeCompletionEvent()
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)

UnityGoogleDrive: Failed to execute authorization procedure. Check application settings and credentials.
UnityGoogleDrive.AuthController:HandleAccessTokenProviderDone(IAccessTokenProvider)
UnityEngine.AsyncOperation:InvokeCompletionEvent()

Hmm, if this only happens when connected to a WiFi, I guess there could be something wrong with the router or firewall (on both router or device) config. Not sure what exactly, though. I’ve updated the repo on github, so it should now log the exact error you get from the google auth reponse. Most likely it will be something about the missing token, but still could be helpful.

Think I got it now. Have to do some more tests. Had to add this. Maybe the newer devices or iOS versions have more security

  <key>NSAppTransportSecurity</key>
  <dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
      <key>127.0.0.1</key>
      <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <key>NSIncludesSubdomains</key>
        <true/>
      </dict>
      <key>localhost</key>
      <dict>
        <key>NSExceptionAllowsInsecureHTTPLoads</key>
        <true/>
        <key>NSIncludesSubdomains</key>
        <true/>
      </dict>
    </dict>
  </dict>