Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Credentials (refresh token not working) #33

Closed
foremtehan opened this issue Aug 7, 2021 · 8 comments · Fixed by #35
Closed

Invalid Credentials (refresh token not working) #33

foremtehan opened this issue Aug 7, 2021 · 8 comments · Fixed by #35

Comments

@foremtehan
Copy link

I thought this issue was only happen in this repo but also happens in nao package i described it here nao-pon/flysystem-google-drive#99

When i uploading for two hours, i mean i pushed 10 jobs in laravel to upload some files in different times, the first hour is ok and after that i will get :

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

I did exactly what was said in this article , i checked the "auto refresh token when expired" but somehow the token will expire after an hour, should i do extra work to refresh the access token ?

@erikn69
Copy link
Contributor

erikn69 commented Aug 9, 2021

@foremtehan try this branch: GoogleDriveAdapter.php, if that works i make a PR

@foremtehan
Copy link
Author

foremtehan commented Aug 10, 2021

@erikn69 i get this exception after one hour:

DomainException: set the JSON service account credentials using Google\Client::setAuthConfig or set the path to your JSON file with the "GOOGLE_APPLICATION_CREDENTIALS" environment variable and call Google\Client::useApplicationDefaultCredentials to refresh a token with assertion.#0 /app/vendor/google/apiclient/src/Client.php(265): Google\Client->fetchAccessTokenWithAssertion()

#1 /app/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(302): Google\Client->refreshTokenWithAssertion()
#2 /app/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(1160): Masbug\Flysystem\GoogleDriveAdapter->refreshToken()
#3 /app/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(1773): Masbug\Flysystem\GoogleDriveAdapter->getItems()
#4 /app/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(1938): Masbug\Flysystem\GoogleDriveAdapter->cachePaths()
#5 /app/vendor/masbug/flysystem-google-drive-ext/src/GoogleDriveAdapter.php(638): Masbug\Flysystem\GoogleDriveAdapter->toVirtualPath()
#6 /app/vendor/league/flysystem/src/Filesystem.php(58): Masbug\Flysystem\GoogleDriveAdapter->has()
#7 /app/vendor/league/flysystem/src/Filesystem.php(99): League\Flysystem\Filesystem->has()
#8 /app/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php(249): League\Flysystem\Filesystem->put()
#10 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Jobs\BackupSubtitle->handle()
#11 /app/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#12 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#13 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#14 /app/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call()
#15 /app/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): Illuminate\Container\Container->call()
#16 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Bus\Dispatcher->Illuminate\Bus\{closure}()
#17 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#18 /app/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): Illuminate\Pipeline\Pipeline->then()
#19 /app/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(120): Illuminate\Bus\Dispatcher->dispatchNow()
#20 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Queue\CallQueuedHandler->Illuminate\Queue\{closure}()
#21 /app/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#22 /app/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(122): Illuminate\Pipeline\Pipeline->then()
#23 /app/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(70): Illuminate\Queue\CallQueuedHandler->dispatchThroughMiddleware()
#24 /app/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(98): Illuminate\Queue\CallQueuedHandler->call()
#25 /app/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(428): Illuminate\Queue\Jobs\Job->fire()
#26 /app/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(378): Illuminate\Queue\Worker->process()
#27 /app/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(172): Illuminate\Queue\Worker->runJob()
#28 /app/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(117): Illuminate\Queue\Worker->daemon()
#29 /app/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101): Illuminate\Queue\Console\WorkCommand->runWorker()
#30 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Illuminate\Queue\Console\WorkCommand->handle()
#31 /app/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#32 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#33 /app/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#34 /app/vendor/laravel/framework/src/Illuminate/Container/Container.php(651): Illuminate\Container\BoundMethod::call()
#35 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call()
#36 /app/vendor/symfony/console/Command/Command.php(299): Illuminate\Console\Command->execute()
#37 /app/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run()
#38 /app/vendor/symfony/console/Application.php(978): Illuminate\Console\Command->run()
#39 /app/vendor/symfony/console/Application.php(295): Symfony\Component\Console\Application->doRunCommand()
#40 /app/vendor/symfony/console/Application.php(167): Symfony\Component\Console\Application->doRun()
#41 /app/vendor/laravel/framework/src/Illuminate/Console/Application.php(92): Symfony\Component\Console\Application->run()
#42 /app/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run()
#43 /app/artisan(37): Illuminate\Foundation\Console\Kernel->handle()
#44 {main}

@erikn69
Copy link
Contributor

erikn69 commented Aug 10, 2021

Ok, this time i found the solution, but you have to change your config

Runtime:       PHP 8.0.5
Configuration: /google_flysystem/phpunit.xml.dist
..                                                                  2 / 2 (100%)
Time: 00:08.995, Memory: 8.00 MB
OK (2 tests, 9 assertions)

First you have to create a google service account and get auth config json file

Then change the old config for the new one ( you can look at this: GoogleDriveAdapterTests.php )
before:

$client->setClientId($config['GOOGLE_DRIVE_CLIENT_ID']);
$client->setClientSecret($config['GOOGLE_DRIVE_CLIENT_SECRET']);
$client->refreshToken($config['GOOGLE_DRIVE_REFRESH_TOKEN']);

after: (here you can add GOOGLE_DRIVE_SERVICE_JSON to .env but if you use a same path of your project, add the path to .gitignore file too, for security)

$client->setAuthConfig($config['GOOGLE_DRIVE_SERVICE_JSON']);  // must be full path to json file(first step)
$client->setApplicationName($config['GOOGLE_DRIVE_SERVICE_APP_NAME'] ?? 'Your Name Application');
$client->setScopes(['https://www.googleapis.com/auth/drive']);

Finally try this branch GoogleDriveAdapter.php and tell me later if works and i will make a PR

@foremtehan
Copy link
Author

foremtehan commented Aug 10, 2021

just one thing. is service account is separated from google drive storage ? i'm seeing files successfully uploaded but cannot see them in google drive

I set up a new gmail account with fresh service account, the files wouldn't store to the main drive, is there anyway that i could see the files in web ui ?

@erikn69
Copy link
Contributor

erikn69 commented Aug 10, 2021

@foremtehan last try GoogleDriveAdapter.php, forget all about create a google service account, and use only default first config

$client->setClientId($config['GOOGLE_DRIVE_CLIENT_ID']);
$client->setClientSecret($config['GOOGLE_DRIVE_CLIENT_SECRET']);
$client->refreshToken($config['GOOGLE_DRIVE_REFRESH_TOKEN']);

@foremtehan
Copy link
Author

Ok let me try that, that service account thing looks complicated https://stackoverflow.com/questions/49663359/how-to-upload-file-to-google-drive-with-service-account-credential

@foremtehan
Copy link
Author

@erikn69 i didnt see any issue thanks for your time <3

@erikn69
Copy link
Contributor

erikn69 commented Aug 10, 2021

Ok then i will do a PR for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants