Skip to content

Instantly share code, notes, and snippets.

@ivanvermeyen
Last active March 15, 2023 05:25
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save ivanvermeyen/cc7c59c185daad9d4e7cb8c661d7b89b to your computer and use it in GitHub Desktop.
Save ivanvermeyen/cc7c59c185daad9d4e7cb8c661d7b89b to your computer and use it in GitHub Desktop.
Setup a Laravel Storage driver with Google Drive API

Get started with Google Drive and Laravel

This gist has moved!

This gist was getting a lot of comments/questions, but since there are no notifications when someone replies to a gist, I've moved the setup instructions and a bunch of sample code to a dedicated Github repo.

API key walkthrough

Step by step instructions on how to get the various keys needed to work with Google Drive can be found on the Github repo as well:

Find examples

Take a look at the commit history to get a quick overview of the demo code I've been adding:

Got questions?

If you have questions or comments, please create an issue on the Github repo. This is easier to follow up and enables others to find the answers as well.

@afrasiyabhaider
Copy link

How to get downloadable link of a file?

@ivanvermeyen
Copy link
Author

ivanvermeyen commented Oct 20, 2019

Hi,

Please create an issue @ https://github.com/ivanvermeyen/laravel-google-drive-demo for any questions.
This keeps it organized and helps other people to find an answer to similar problems.

Also, make sure you go through open and closed issues first, as your question may already have been asked/answered. 👍

I will be happy to help you out if I can!

https://github.com/ivanvermeyen/laravel-google-drive-demo

@toanVini
Copy link

I have been using Google Drive and Laravel in my project but I had problems downloading files larger than 500 Mb. But the download ends at 500 MB to stop. Is there a way to deal with downloading files that are 1 GB - 5 GB in size? Thank you

@myazidinniam
Copy link

myazidinniam commented Mar 18, 2021

@ivanvermeyen,
New issue, after finishing all steps got error when call 'test' route which should create a file:

(1/1) Google_Service_Exception
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}

ClientId, Secret, Refresh token are set correctly

You can solve this problem with update your env file on section GOOGLE_REFRESH_TOKEN with separator on value
example GOOGLE_REFRESH_TOKEN="1//04xd08cKBcYDWCgYIARAAGAQSNwF-L9IruTrG9cNMQajASodNAnuoYRPsPWpbJxxxxxxxxxxxx"
another that
Screen Shot 2021-03-18 at 16 05 31

@tranhoandz98
Copy link

how to get body code in file REST.php line 151 to 142.

public static function decodeHttpResponse(
ResponseInterface $response,
RequestInterface $request = null,
$expectedClass = null
) {
$code = $response->getStatusCode();

// retry strategy
if (intVal($code) >= 400) {
  // if we errored out, it should be safe to grab the response body
  $body = (string) $response->getBody();

  // Check if we received errors, and add those to the Exception for convenience
  throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body));
}

// Ensure we only pull the entire body into memory if the request is not
// of media type
$body = self::decodeBody($response, $request);

if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) {
  $json = json_decode($body, true);

  return new $expectedClass($json);
}

return $response;

}

@Gowthamhm
Copy link

In the Git Repo https://github.com/ivanvermeyen/laravel-google-drive-demo , you call the google drive api calls in the web.php(routes) only.
Please give a some example or provide some resources to call the drive storage api call in controllers also.

This repo helped a lot for me, Thanks for creating this repo.
Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment