使用 Google 驱动器 API 和 PHP 上传图片?
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 密钥来自 APIs & auth->Credentials
下的 Google 开发者控制台,它是一个 Public API 密钥。
任何帮助将不胜感激!
谢谢
编辑: DriveClient
是包含函数
的 class
public api 键用于 public 访问 API。不属于用户的数据。您需要从 Web 应用程序创建客户端 ID。
然后您将能够使用 Oauth2 对其进行身份验证。
这可能会让您入门。 drive Quickstart php
如何知道何时需要进行身份验证:
Public API: Aanalytics metadata API 元数据不属于任何用户其 public 任何人都可以访问的数据。
用户数据API: Google Analytics Real-time Data返回的数据属于用户所有,不是public所有人都可以查看的数据。文档状态
Requires authorization
如果文档说需要身份验证,那么您必须经过身份验证才能访问该调用。
父文件
检查 $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();
}
}
我正在尝试使用 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 密钥来自 APIs & auth->Credentials
下的 Google 开发者控制台,它是一个 Public API 密钥。
任何帮助将不胜感激!
谢谢
编辑: DriveClient
是包含函数
public api 键用于 public 访问 API。不属于用户的数据。您需要从 Web 应用程序创建客户端 ID。
然后您将能够使用 Oauth2 对其进行身份验证。
这可能会让您入门。 drive Quickstart php
如何知道何时需要进行身份验证:
Public API: Aanalytics metadata API 元数据不属于任何用户其 public 任何人都可以访问的数据。
用户数据API: Google Analytics Real-time Data返回的数据属于用户所有,不是public所有人都可以查看的数据。文档状态
Requires authorization
如果文档说需要身份验证,那么您必须经过身份验证才能访问该调用。
父文件
检查 $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();
}
}