Dn Sh
Dn Sh

Reputation: 9

Refresh token must be passed in or set as part of setAccessToken

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.

Upvotes: 0

Views: 606

Answers (1)

BenBJ
BenBJ

Reputation: 21

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()
{
    //
}
}

Upvotes: 2

Related Questions