使用带有PHP的Google Drive API上传图片?

时间:2015-06-26 11:45:57

标签: php api google-drive-api

我正在尝试使用Google API(How to get Docker to run on a Windows System behind a corporate firewall?)上传图片,而我似乎无法使其正常运行。每当我运行它时,它会给我401 Login Required错误。这是我的代码:

功能:

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

调用函数:

$client = new DriveClient();

foreach($dm->entities->media as $img)
{
    Printer::write('Recieved image from "' . $dm->sender->screen_name . '", saving to Drive...');

    $client->upload($this->getImage($img->media_url), $dm->sender->screen_name);
}

错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with 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

我使用的API密钥来自APIs & auth->Credentials下的Google Developers控制台,它是一个公共API密钥。

任何帮助将不胜感激!

由于

编辑: DriveClient是包含函数的类

1 个答案:

答案 0 :(得分:3)

公共API密钥用于公共访问API。不属于用户的数据。您需要从Web应用程序创建客户端ID。

然后,您将能够使用Oauth2对其进行身份验证。

这可能会让你开始。 drive Quickstart php

如何知道何时需要进行身份验证:

  

需要授权

如果文档说明需要身份验证,那么您必须通过身份验证才能访问该呼叫。

档案家长

检查$ parentId变量,如果您没有添加setParents,则将文件上传到根文件夹中,这是将文件上传到特定目录的方式。 code ripped from

/**
 * Insert new file.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @param string $title Title of the file to insert, including the extension.
 * @param string $description Description of the file to insert.
 * @param string $parentId Parent folder's ID.
 * @param string $mimeType MIME type of the file to insert.
 * @param string $filename Filename of the file to insert.
 * @return Google_Service_Drive_DriveFile The file that was inserted. NULL is
 *     returned if an 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 the 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 the following line to print the File ID
    // print 'File ID: %s' % $createdFile->getId();

    return $createdFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}