0

I am trying to upload file to google drive using drive API. I am doing this first time so I don't know much about this. I have set up the .env file. Here is the modifications i have made in .env file

APP_TITLE=dsse
APP_TIMEZONE="Asia/Manila"
GOOGLE_CLIENT_ID="1029303670830-u70757ll7qdnhtehsrai06uld6u2p8g0.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="SI-oG6tSOg6S_jaeveyX-Avh"
GOOGLE_REDIRECT_URL="http://127.0.0.1:8000/members"
GOOGLE_SCOPES="email,profile,https://www.googleapis.com/auth/drive"
GOOGLE_APPROVAL_PROMPT="force"
GOOGLE_ACCESS_TYPE="offline"

My controller is here

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Response;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use App\Publication as Publications;
use Session;
use App\Google;


class PublicationController extends Controller
{
private $client;
    private $drive;

    public function __construct(Google $googl)
    {
        $this->client = $googl->client();
        $this->client->setAccessToken(session('user.token'));
        $this->drive = $googl->drive($this->client);
    }

    public function storeFile(Request $request){
        if($request->ajax()){
            if($request->file('file')){
                $this->validate($request, [
                    'file'=> 'required|mimes:docx,doc,pdf|max:10048',
                ]);

                $file = $request->file('file');
                $mime_type = $file->getMimeType();
                $fileName = time().'.'.$request->file('file')->getClientOriginalExtension();

                $drive_file = new \Google_Service_Drive_DriveFile();
                $drive_file->setName($fileName);
                $drive_file->setMimeType($mime_type);

                try {
                    $createdFile = $this->drive->files->create($drive_file, [
                        'data' => $file,
                        'mimeType' => $mime_type,
                        'uploadType' => 'multipart'
                    ]);

                    $file_id = $createdFile->getId();
                    echo $fileName;

                } catch (Exception $e) {

                    echo "Error occured when uploading file in drive";

                }
            }
        }
        else echo "not ajax";
    }
}

The Google class is as following

<?php

namespace App;

class Google
{
    public function client()
    {
        $client = new \Google_Client();
        $client->setClientId(env('GOOGLE_CLIENT_ID'));
        $client->setClientSecret(env('GOOGLE_CLIENT_SECRET'));
        $client->setRedirectUri(env('GOOGLE_REDIRECT_URL'));
        $client->setScopes(explode(',', env('GOOGLE_SCOPES')));
        $client->setApprovalPrompt(env('GOOGLE_APPROVAL_PROMPT'));
        $client->setAccessType(env('GOOGLE_ACCESS_TYPE'));

        return $client;
    }


    public function drive($client)
    {
        $drive = new \Google_Service_Drive($client);
        return $drive;
    }
}

When I run the script i get the following error

InvalidArgumentException in Client.php line 429: invalid json token

What is the problem? How to solve?

Mutasim Fuad
  • 606
  • 2
  • 12
  • 31
  • I'd suggest you to remove your `GOOGLE_CLIENT_SECRET` from your post. Otherwise ppl can abuse your credentials! – manniL Mar 19 '17 at 19:09
  • Too late. I suggest you delete that credential and create a new one. – pinoyyid Mar 19 '17 at 22:31
  • Which is line 429? – pinoyyid Mar 19 '17 at 22:31
  • Follow the guide on how to obtain the credentials in the [PHP Quickstart](https://developers.google.com/drive/v3/web/quickstart/php). You'll be downloading your cliente_secret.json into your working directory. After that use the code snippet provided for PHP in [Uploading Files](https://developers.google.com/drive/v3/web/manage-uploads). – ReyAnthonyRenacia Mar 19 '17 at 23:41

0 Answers0