Using Google Drive API with PHP to upload images? -
i trying upload images google api (https://github.com/google/google-api-php-client) , can't seem working. whenever run it gives me 401 login required
error. here code:
functions:
public function __construct() { $this->instance = new \google_client(); $this->instance->setapplicationname('dpstatsbot'); $this->instance->setdeveloperkey(config::getinstance()->getdrivedeveloperkey()); $this->instance->addscope('https://www.googleapis.com/auth/drive'); $this->drive_instance = new \google_service_drive($this->instance); } public function upload($image, $dpname) { $file = new \google_service_drive_drivefile(); $file->settitle($dpname . '_' . randomstring::string()); $upload = $this->drive_instance->files->insert($file, [ 'data' => $image, 'mimetype' => 'image/jpg', 'uploadtype' => 'media' ]); return $upload; }
calls functions:
$client = new driveclient(); foreach($dm->entities->media $img) { printer::write('recieved image "' . $dm->sender->screen_name . '", saving drive...'); $client->upload($this->getimage($img->media_url), $dm->sender->screen_name); }
error:
fatal error: uncaught exception 'google_service_exception' message 'error c alling post https://www.googleapis.com/upload/drive/v2/files?uploadtype=multipar t&key=apikeyhere: (401) login required' in c:\doesp lay\dpstatsbot\vendor\google\apiclient\src\google\http\rest.php:110
the api key using google developers console under apis & auth->credentials
, public api key.
any appreciated!
thanks
edit: driveclient
class contains functions
the public api key public access apis. data not owned user. need create client id web application.
then able authenticate using oauth2.
this might started. drive quickstart php
how know when need authenticated:
public api: aanalytics metadata api meta data doesn't belong user public data can access.
user data api: google analytics real-time data data returned owned user, not public data viewable everyone. documentation states
requires authorization
if documentation says requires authentication have authenticated access call.
file parent
check $parentid variable, how upload file specific directory if don't add setparents uploaded root folder. code ripped from
/** * insert new file. * * @param google_service_drive $service drive api service instance. * @param string $title title of file insert, including extension. * @param string $description description of file insert. * @param string $parentid parent folder's id. * @param string $mimetype mime type of file insert. * @param string $filename filename of file insert. * @return google_service_drive_drivefile file inserted. null * returned if api error occurred. */ function insertfile($service, $title, $description, $parentid, $mimetype, $filename) { $file = new google_service_drive_drivefile(); $file->settitle($title); $file->setdescription($description); $file->setmimetype($mimetype); // set parent folder. if ($parentid != null) { $parent = new google_service_drive_parentreference(); $parent->setid($parentid); $file->setparents(array($parent)); } try { $data = file_get_contents($filename); $createdfile = $service->files->insert($file, array( 'data' => $data, 'mimetype' => $mimetype, )); // uncomment following line print file id // print 'file id: %s' % $createdfile->getid(); return $createdfile; } catch (exception $e) { print "an error occurred: " . $e->getmessage(); } }
Comments
Post a Comment