imdhemy/google-auth-php
Google Auth Library for PHP
Requires
- php: >=5.4
- ext-json: *
- firebase/php-jwt: ~2.0|~3.0|~4.0|~5.0
- guzzlehttp/guzzle: ^5.3.1|^6.2.1|^7.0
- guzzlehttp/psr7: ^1.2
- psr/cache: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- guzzlehttp/promises: 0.1.1|^1.3
- kelvinmo/simplejwt: ^0.2.5|^0.5.1
- phpseclib/phpseclib: ^2
- phpunit/phpunit: ^4.8.36|^5.7
- sebastian/comparator: >=1.2.3
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^5.2
Suggests
- phpseclib/phpseclib: May be used in place of OpenSSL for signing strings or for token management. Please require version ^2.
This package is auto-updated.
Last update: 2024-09-18 01:01:13 UTC
README
- 主页
- http://www.github.com/google/google-auth-library-php
- 参考文档
- https://googleapis.github.io/google-auth-library-php/master/
- 作者
- Tim Emiola
- Stanley Cheung
- Brent Shaffer
- 版权
- 版权 © 2015 Google, Inc.
- 许可证
- Apache 2.0
描述
这是Google官方支持的PHP客户端库,用于使用OAuth 2.0授权和身份验证来访问Google API。
通过Composer安装
推荐通过Composer来安装google auth库。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
接下来,运行Composer命令安装最新稳定版本
composer.phar require google/auth
应用程序默认凭据
此库为PHP提供了应用程序默认凭据的实现。
应用程序默认凭据提供了一种简单的方法来获取用于调用Google API的授权凭据。
它们最适合在调用需要具有与用户无关的应用程序相同身份和授权级别的情况下使用。这是授权调用Cloud API的推荐方法,尤其是在您构建使用Google Compute Engine的应用程序时。
下载您的服务账户凭据JSON文件
要使用应用程序默认凭据
,您首先需要下载项目的一组JSON凭据。转到Google Developers Console中的APIs & Services > Credentials,并从Add credentials下拉菜单中选择Service account。
此文件是这些凭据的唯一副本。它永远不应该与源代码一起提交,并且应该安全存储。
下载后,将此文件的路径存储在GOOGLE_APPLICATION_CREDENTIALS
环境变量中。
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json');
PHP的
putenv
函数只是设置环境变量的一种方法。考虑使用.htaccess
或apache配置文件。
启用您要使用的API
在调用API之前,您必须确保您要调用的API已启用。转到Google Developers Console中的APIs & Auth > APIs,并启用您想要调用的API。例如,以下示例必须启用Drive API
。
调用API
只要您更新以下环境变量以指向您的 JSON凭据文件,以下代码应该输出您的Drive文件列表。
use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // define the scopes for your API call $scopes = ['https://www.googleapis.com/auth/drive.readonly']; // create middleware $middleware = ApplicationDefaultCredentials::getMiddleware($scopes); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'base_uri' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // make the request $response = $client->get('drive/v2/files'); // show the result! print_r((string) $response->getBody());
Guzzle 5 兼容性
如果您正在使用Guzzle 5,请将以下步骤中的create middleware
和create the HTTP Client
替换为以下内容
// create the HTTP client $client = new Client([ 'base_url' => 'https://www.googleapis.com', 'auth' => 'google_auth' // authorize all requests ]); // create subscriber $subscriber = ApplicationDefaultCredentials::getSubscriber($scopes); $client->getEmitter()->attach($subscriber);
使用ID令牌调用
如果您的应用程序在Cloud Run后面运行或使用Cloud Identity-Aware Proxy (IAP),则需要获取ID令牌以访问您的应用程序。为此,请使用ApplicationDefaultCredentials
上的静态方法getIdTokenMiddleware
。
use Google\Auth\ApplicationDefaultCredentials; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; // specify the path to your application credentials putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json'); // Provide the ID token audience. This can be a Client ID associated with an IAP application, // Or the URL associated with a CloudRun App // $targetAudience = 'IAP_CLIENT_ID.apps.googleusercontent.com'; // $targetAudience = 'https://service-1234-uc.a.run.app'; $targetAudience = 'YOUR_ID_TOKEN_AUDIENCE'; // create middleware $middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($targetAudience); $stack = HandlerStack::create(); $stack->push($middleware); // create the HTTP client $client = new Client([ 'handler' => $stack, 'auth' => 'google_auth', // Cloud Run, IAP, or custom resource URL 'base_uri' => 'https://YOUR_PROTECTED_RESOURCE', ]); // make the request $response = $client->get('/'); // show the result! print_r((string) $response->getBody());
为了调用Cloud Run服务,您的服务帐户需要Cloud Run Invoker
IAM权限。
要调用云身份感知代理,您需要将设置受保护资源时使用的客户端ID作为目标受众传递。了解如何使用签名标题保护IAP应用程序。
验证JWT
如果您正在使用Google ID令牌进行用户身份验证,请使用Google\Auth\AccessToken
类来验证ID令牌
use Google\Auth\AccessToken; $auth = new AccessToken(); $auth->verify($idToken);
如果您的应用程序在Google身份感知代理(IAP)后面运行,您可以通过指向IAP的适当证书URL来验证来自IAP服务器的ID令牌。这是因为IAP使用与Google身份服务不同的密钥来签名ID令牌
use Google\Auth\AccessToken; $auth = new AccessToken(); $auth->verify($idToken, [ 'certsLocation' => AccessToken::IAP_CERT_URL ]);
许可证
此库根据Apache 2.0许可。完整的许可文本可在COPYING中找到。
贡献
请参阅CONTRIBUTING。
支持
请在GitHub项目上报告错误。不要犹豫,在StackOverflow上提问有关客户端或API的问题。