Google Drive PHP API - Simple File Upload

后端 未结 3 1832
情歌与酒
情歌与酒 2020-11-27 04:01

I am trying to write a small script to upload a local file to Google Drive, using Google Drive PHP API. The documentation is very poor maintained, but so far I am pretty sur

3条回答
  •  迷失自我
    2020-11-27 04:26

    Use this

    setClientId('YOUR_CLIENT_ID');
        $client->setClientSecret('YOUR_CLIENT_SECRET');
        $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));
    
        $service = new Google_DriveService($client);
    
        $authUrl = $client->createAuthUrl();
    
        //Request authorization
        print "Please visit:\n$authUrl\n\n";
        print "Please enter the auth code:\n";
        $authCode = trim(fgets(STDIN));
    
        // Exchange authorization code for access token
        $accessToken = $client->authenticate($authCode);
        $client->setAccessToken($accessToken);
    
        //Insert a file
        $file = new Google_DriveFile();
        $localfile = 'a.jpg';
        $title = basename($localfile);
        $file->setTitle($title);
        $file->setDescription('My File');
        $file->setMimeType('image/jpeg');
    
        $data = file_get_contents($localfile);
    
        $createdFile = $service->files->insert($file, array(
              'data' => $data,
              'mimeType' => 'image/jpeg',
            ));
    
        print_r($createdFile);
        ?>
    

提交回复
热议问题