InvalidArgumentException Laravel 5

时间:2017-03-19 17:32:32

标签: php laravel-5 google-drive-api google-drive-realtime-api invalidargumentexception

我正在尝试使用驱动器API将文件上传到Google驱动器。我这是第一次这样做,所以我对此并不了解。 我已经设置了 .env 文件。以下是我在 .env 文件

中所做的修改
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"

我的控制器在这里

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";
    }
}

Google类如下

<?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;
    }
}

当我运行脚本时,我收到以下错误

  

Client.php第429行中的InvalidArgumentException:无效的json标记

有什么问题?怎么解决?

0 个答案:

没有答案