w3lifer/php-google-drive

1.0.1 2024-09-01 11:09 UTC

This package is auto-updated.

Last update: 2024-09-26 17:55:18 UTC


README

安装

composer require w3lifer/php-google-drive

使用

<?php

// require_once __DIR__ . '/vendor/autoload.php';

use w3lifer\google\Drive;

$googleDrive = new Drive([
    'pathToCredentials' => __DIR__ . '/credentials.json', // Required
    'pathToToken' => __DIR__ . '/token.json', // Required
]);

$fileId = $googleDrive->upload(
    __DIR__ . '/hello.txt',  // Required
    [ // Optional
        '<folder id>',
        '<folder id>',
    ]
);
  • 文件夹ID: https://drive.google.com/drive/folders/<folder-id>

如何获取Google Drive API凭证

创建一个新的项目

启用Google Drive API

创建凭证

如果你离开了上面的屏幕,请转到“启用API & 服务”并点击“Google Drive API”

发布应用

如何获取令牌

1. 将凭证保存到磁盘,并通过设置pathToCredentials配置键指定它们的路径

$googleDrive = new Drive([
    'pathToCredentials' => __DIR__ . '/credentials.json', // Required
    'pathToToken' => __DIR__ . '/token.json', // Required
]);

注意 pathToTokenDrive在首次运行后保存令牌的路径。也就是说,令牌将自动保存到指定的路径;这对于你的应用在随后的访问Google Drive时无需同意界面是必需的。

2. 运行你的应用

  • 你将在控制台中收到以下消息
Open the following link in your browser:
https://#/o/oauth2/v2/auth?...
Enter verification code:
  • 打开消息中的链接并选择所需的帐户

  • 可能会出现“Google尚未验证此应用”的警告。点击“高级”然后点击“前往 ... (不安全)”

  • 在下一个屏幕上点击“继续”

  • 重定向后,从浏览器地址栏复制代码并粘贴到控制台

Open the following link in your browser:
https://#/o/oauth2/v2/auth?...
Enter verification code: <here>

3. 享受!