简体   繁体   English

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

[英]Using Google Drive API with PHP to upload images?

I am trying to upload images with the Google API ( https://github.com/google/google-api-php-client ) and I can't seem to get it working. 我正在尝试使用Google API( https://github.com/google/google-api-php-client )上传图片,而我似乎无法使其正常运行。 Whenever I run it it gives me a 401 Login Required error. 每当我运行它时,它会给我一个401 Login Required错误。 Here is my 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 to functions: 调用函数:

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

Error: 错误:

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

The API key I am using is from my Google Developers console under APIs & auth->Credentials and it is a Public API key. 我使用的API密钥来自我的Google Developers控制台,其中包含APIs & auth->Credentials ,它是一个公共API密钥。

Any help will be greatly appreciated! 任何帮助将不胜感激!

Thanks 谢谢

Edit: DriveClient is the class that contains the functions 编辑: DriveClient是包含这些函数的类

The public api key is for public access APIs. 公共API密钥用于公共访问API。 Data that is not owned by a user. 不属于用户的数据。 You need to create client id from web application. 您需要从Web应用程序创建客户端ID。

Then you will be able to authenticate it using Oauth2. 然后,您将能够使用Oauth2对其进行身份验证。

this might get you started. 这可能会让你开始。 drive Quickstart php 驱动Quickstart php

How to know when you need to be authenticated: 如何知道何时需要进行身份验证:

  • Public API: Aanalytics metadata API meta data doesn't belong to any user its public data anyone can access. 公共API: Aanalytics元数据API元数据不属于任何用户可以访问的公共数据。

  • User data API: Google Analytics Real-time Data the data returned is owned by a user, it is not public data viewable by everyone. 用户数据API: Google Analytics实时数据返回的数据归用户所有,不是每个人都可以查看的公共数据。 Documentation states 文档说明

Requires authorization 需要授权

If the documentation says requires authentication then you have to be authenticated to access that call. 如果文档说需要身份验证,则必须通过身份验证才能访问该调用。

File Parent 文件父

Check the $parentId variable, this is how you upload a file into a specific directory if you don't add setParents it will be uploaded into the root folder. 检查$ 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();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM