unity-game-enginegoogle-drive-apiunity-webgl

Error 403 "forbidden" being thrown on unity web application


Recently (within past week) our unity webgl (Unity v2019.4.16f1) application has been unable to read files using the google drive API. When I check the web console I see the following error:

UnityGoogleDrive: HTTP/1.1 403 Forbidden Google Drive API Error Description: Code '403' Message: 'The request is missing a valid API key.' Domain: 'global' Reason: 'forbidden' Message: 'The request is missing a valid API key.' LocationType: '' Location: '' (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

The application has been in place for 4 months without issue until now. We currently have an OAuth 2.0 client ID setup, but no API key as we haven't needed it before. The plugin we are using for google drive access in Unity (https://github.com/Elringus/UnityGoogleDrive) does not have any obvious settings for adding an API key in addition to the OAuth ID.

When running the application directly in Unity (which uses localhost) there are no errors and files can be accessed.

I have tried disabling and re-enabling the API in the cloud console. This has given mixed results. The initial request on the web application after re-enabling the Google Drive API succeeds without any errors. All subsequent requests fail with the same error.

Based on the missing values I thought the issue might be that we needed a valid billing plan assigned to the google project but that has not resolved the issue either.

I have tried generating an API key but I am not sure where I need to pass it in order to resolve the issue (if that is even the cause of the issue).


Solution

  • The error reported was a red herring. The real issue was that our request was not triggering authentication with Google. Our Unity dev environment had cached an authentication token which is why the dev environment functioned but the production app failed.

    We resolved the issue by sending a request to the GoogleDriveAbout API before anything else, which seems to reliably trigger reauthentication. Once authenticated everything works as expected.