首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用和PHP上传图片?

使用和PHP上传图片?
EN

Stack Overflow用户
提问于 2015-06-26 11:45:56
回答 1查看 3.6K关注 0票数 1

我正试图用Google (https://github.com/google/google-api-php-client)上传图片,但我似乎无法做到这一点。每当我运行它时,它都会给我一个401 Login Required错误。这是我的代码:

功能:

代码语言:javascript
复制
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;
}

对职能的调用:

代码语言:javascript
复制
$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);
}

错误:

代码语言:javascript
复制
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下的,它是一个公共API密钥。

任何帮助都将不胜感激!

谢谢

编辑: DriveClient是包含函数的类

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-26 12:23:50

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

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

这可能会让你开始。驱动Quickstart php

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

  • 公共API:分析元数据API元数据不属于任何用户,它是任何人都可以访问的公共数据。
  • 用户数据API:谷歌分析实时数据返回的数据是用户拥有的,它不是每个人都可以查看的公共数据。文献状态

需要授权

如果文档中说需要身份验证,那么必须对您进行身份验证才能访问该调用。

文件父级

检查$parentId变量,这就是如何将文件上传到特定目录,如果不添加setParents,它将被上传到根文件夹中。代码从

代码语言:javascript
复制
/**
 * 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();
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31072388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档