如何在不使用路径的情况下设置 GOOGLE_APPLICATION_CREDENTIALS

RWS*_*RWS 5 php environment-variables oauth-2.0 google-oauth

我正在开发一个 CMS 模块,需要使用 Google OAUTH 2 作为服务器到服务器应用程序。根据官方手册,需要设置一个环境变量,其路径为 .json 键,如下所示:

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
Run Code Online (Sandbox Code Playgroud)

这是棘手的部分。如果我在独立的 Web 应用程序中使用它,那么就不会有任何问题,但由于我正在开发 CMS 模块,将该文件存储在驱动器上或创建与使用相关的任何类型的挂钩该文件的内容会构成潜在的安全威胁,因为我已经看到如何通过使用另一个模块来偷偷窃取密钥的内容。

我想将此文件的内容存储在数据库中,问题是:有没有一种方法可以在不使用路径的情况下设置 GOOGLE_APPLICATION_CREDENTIALS 的环境值?

小智 2

在初始化客户端时,可以使用 keyFile 密钥作为配置选项接受。

示例代码取自官方 api 文档 - https://github.com/googleapis/google-cloud-php

require 'vendor/autoload.php';

use Google\Cloud\Core\ServiceBuilder;

// Authenticate using a keyfile path
$cloud = new ServiceBuilder([
    'keyFilePath' => 'path/to/keyfile.json'
]);

// Authenticate using keyfile data
$cloud = new ServiceBuilder([
    'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);
Run Code Online (Sandbox Code Playgroud)

可以使用任何谷歌客户端来代替 ServiceBuilder。