簡體   English   中英

使用帶有PHP的Google Drive API上傳圖片?

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

我正在嘗試使用Google API( https://github.com/google/google-api-php-client )上傳圖片,而我似乎無法使其正常運行。 每當我運行它時,它會給我一個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密鑰來自我的Google Developers控制台,其中包含APIs & auth->Credentials ,它是一個公共API密鑰。

任何幫助將不勝感激!

謝謝

編輯: DriveClient是包含這些函數的類

公共API密鑰用於公共訪問API。 不屬於用戶的數據。 您需要從Web應用程序創建客戶端ID。

然后,您將能夠使用Oauth2對其進行身份驗證。

這可能會讓你開始。 驅動Quickstart php

如何知道何時需要進行身份驗證:

  • 公共API: Aanalytics元數據API元數據不屬於任何用戶可以訪問的公共數據。

  • 用戶數據API: Google Analytics實時數據返回的數據歸用戶所有,不是每個人都可以查看的公共數據。 文檔說明

需要授權

如果文檔說需要身份驗證,則必須通過身份驗證才能訪問該調用。

文件父

檢查$ parentId變量,這是你如何將文件上傳到特定目錄,如果你沒有添加setParents,它將被上傳到根文件夾。 代碼被撕掉了

/**
 * 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