digitalzenworks/google-api-authorization

Google API 授权包文件

1.2.4 2022-08-31 06:06 UTC

This package is auto-updated.

Last update: 2024-09-12 09:36:55 UTC


README

一个简化Google API服务授权的库。

入门指南

需求/依赖

安装

Git

git clone https://github.com/jamesjohnmcguire/GoogleApiAuthorizationPhp

Composer

composer require https://packagist.org.cn/packages/digitalzenworks/google-api-authorizer

使用

有一个主要类和一个主要静态方法。您可以指定授权模式,例如'通过令牌'或'服务账户'。您还可以使用 Mode::Discover,它将尝试通过几种不同的授权类型直到成功。如果所有自动模式都失败且 promptUser 为 true,它将尝试一些用户交互模式。您可以像这样调用

require_once  'vendor/autoload.php';

use DigitalZenWorks\GoogleApiAuthorization\GoogleAuthorization;
use DigitalZenWorks\GoogleApiAuthorization\Mode;

$client = Authorizer::authorize(
	Mode::Discover,
	$credentialsFilePath,
	$serviceAccountFilePath,
	$tokensFilePath,
	'Google Drive API File Uploader',
	['https://www.googleapis.com/auth/drive'],
	'https://:8000/test.php',
	['promptUser' => false, 'showWarnings' => false]);

if ($client === null)
{
	echo 'Oops, authorization failed';
}
else
{
	echo 'Client is initialized and authorized, lets go';
}

主方法参数包括

不同的模式包括

不同的选项包括

其他示例

您可以直接调用授权模式,例如

		$client = Authorizer::authorizeServiceAccount(
			$serviceAccountFilePath,
			'Google Drive API File Uploader',
			['https://www.googleapis.com/auth/drive'],
			false);

		$client = Authorizer::authorizeToken(
			$credentialsFilePath,
			$tokensFilePath,
			'Google Drive API File Uploader',
			['https://www.googleapis.com/auth/drive'],
			false);

使用实例化的对象

		$authorizer = new Authorizer('Google Drive API File Uploader',
		['https://www.googleapis.com/auth/drive'],
		['promptUser' => false, 'showWarnings' => false]);

		$client = $authorizer->apiAuthorize(
			Mode::Discover,
			$this->credentialsFilePath,
			$this->serviceAccountFilePath,
			$this->tokensFilePath,
			'https://:8000/test.php');

查看 tests.php 或 UnitTests.php 文件以获取更多示例。

其他说明

即使在使用'服务账户'模式时,服务账户文件也是可选的。如果不存在有效的文件,它将尝试使用环境变量 GOOGLE_APPLICATION_CREDENTIALS 中指定的文件。

贡献

如果您有改进建议,请分叉仓库并创建一个 pull request。您也可以简单地创建一个带有“enhancement”标签的问题。

流程

  1. 分叉项目
  2. 创建功能分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m '添加一些AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开 pull request

编码风格

请匹配当前的编码风格。最值得注意的是

  1. 每行一个操作
  2. 在变量和方法名称中使用完整的英文单词
  3. 尝试以自说明的方式声明变量和方法名称

许可

在 MIT 许可下分发。有关更多信息,请参阅 LICENSE

联系

James John McGuire - @jamesmc - jamesjohnmcguire@gmail.com

项目链接: https://github.com/jamesjohnmcguire/GoogleApiAuthorizationPhp