elringus / unity-google-drive

Google Drive SDK for Unity game engine
https://forum.unity.com/threads/515360
MIT License
404 stars 64 forks source link

Add an option to determine the need of prompting for permissions #137

Closed nicolasboattini closed 9 months ago

nicolasboattini commented 10 months ago

I finally figured it out. While reviewing your latest commits on the fork I created, I came across the with following code in the file "LoopbackAccessTokenProvider.cs" at "Assets/UnityGoogleDrive/Runtime/Authorization/":

// Create the OAuth 2.0 authorization request.
// https://developers.google.com/identity/protocols/OAuth2WebServer#creatingclient

            var authRequest = string.Format(
                "{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}&state={4}&code_challenge={5}&code_challenge_method={6}" +
                "&access_type=offline" + // Forces to return a refresh token at the auth code exchange phase.
                "&approval_prompt=force", // Forces to show consent screen for each auth request. Needed to return refresh tokens on consequent auth runs.
                settings.GenericClientCredentials.AuthUri,
                Uri.EscapeDataString(settings.AccessScope),
                Uri.EscapeDataString(redirectUri),

I attempted to change the parameter 'approval_prompt' to 'auto', and that resolved everything – no more prompting for permissions once accepted previously. So, I set up a response loopback HTML with a script to close the window upon redirection, and it worked like a charm!

I also configured Edge as the default browser (since Chrome wouldn't let me close the window via script) and logged into the only account that will be uploading the files.

Perhaps it would be useful to add a selection field in the configurations to choose the values for 'approval_prompt' and 'access_type'.

elringus commented 10 months ago

Iirc, there is no need to send auth request if you have refresh token; instead send refresh request to update the auth token. And &approval_prompt=force is required to get the refresh token on the initial auth request. Check the sample project: it doesn't ask for auth after it was completed initially.

github-actions[bot] commented 10 months ago

This issue is stale because it has been open 14 days with no activity. It will be automatically closed in 7 days.