0

An error occurred with the token transfer. Laravel, Google Drive API

Provider Code

class GoogleDriveServiceProvider extends ServiceProvider
{

    public function register()
    {
        //
    }
    
    public function boot()
    {
        Storage::extend('google', function($app, $config) {
            $client = new \Google_Client();
            $client->setClientId($config['clientId']);
            $client->setClientSecret($config['clientSecret']);
            $client->refreshToken($config['refreshToken']);
            $client->setAccessType('offline');
            $client->setApprovalPrompt('force');

            $service = new \Google_Service_Drive($client);
            $adapter = new GoogleDriveAdapter($service, $config['folderId']);

            return new Filesystem($adapter);
        });
    }
}

Error

When uploading a project to Heroku, this error occurs

{ "message": "refresh token must be passed in or set as part of setAccessToken" "exception": "LoginExpception", ... }

Please tell me how to solve this problem, thank you in advance.

ParisaN
  • 1,816
  • 2
  • 23
  • 55
Dn Sh
  • 9
  • 1
  • 1
    Maybe this can help: https://stackoverflow.com/questions/39314833/google-api-client-refresh-token-must-be-passed-in-or-set-as-part-of-setaccessto – Douma Nov 08 '21 at 18:41

1 Answers1

2

Reading your code, I supposed you have followed this tutorial :

"Setup a Laravel Storage driver with Google Drive API"

https://gist.github.com/sergomet/f234cc7a8351352170eb547cccd65011

I had a similar issue. After trying multiple solutions, I have followed this tutorial :

https://robindirksen.com/blog/google-drive-storage-as-filesystem-in-laravel

And replaced the GoogleDriveServiceProvider with :

<?php

namespace App\Providers;

use Google_Client;
use Hypweb\Flysystem\GoogleDrive\GoogleDriveAdapter;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Storage;

class GoogleDriveServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    Storage::extend('google', function($app, $config) {
        $client = new Google_Client();
        $client->setClientId($config['clientId']);
        $client->setClientSecret($config['clientSecret']);
        $client->refreshToken($config['refreshToken']);
        $service = new \Google_Service_Drive($client);

        $options = [];
        if(isset($config['teamDriveId'])) {
            $options['teamDriveId'] = $config['teamDriveId'];
        }

        $adapter = new GoogleDriveAdapter($service, $config['folderId'], $options);

        return new Filesystem($adapter);
    });
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
BenBJ
  • 21
  • 3