如何使用OAuth2在PHP中对Google Cloud API进行身份验证

如何使用OAuth2在PHP中对Google Cloud API进行身份验证,php,api,google-cloud-platform,oauth-2.0,Php,Api,Google Cloud Platform,Oauth 2.0,我正在尝试使用这些软件包与谷歌云API进行交互 例如,我在这里发出一个账单账户请求: $client = new CloudBillingClient(); $accounts = $client->listBillingAccounts(); foreach ($accounts as $account) { print('Billing account: ' . $account->getName() . PHP_EOL); } 这显然不起作用,因为没有凭据,并引发错

我正在尝试使用这些软件包与谷歌云API进行交互

例如,我在这里发出一个账单账户请求:

$client = new CloudBillingClient();
$accounts = $client->listBillingAccounts();

foreach ($accounts as $account) {
    print('Billing account: ' . $account->getName() . PHP_EOL);
}
这显然不起作用,因为没有凭据,并引发错误:

Google\ApiCore\ValidationException
Could not construct ApplicationDefaultCredentials 
那么,如何进行身份验证以使账单查询正常工作呢?明确地说,我可以使用另一个OAuth2库来获取访问令牌,没有问题。例如,在此Laravel控制器中:

use League\OAuth2\Client\Provider\Google as GoogleProvider;

class GoogleAuthController extends Controller
{
    public function index()
    {
        $provider = new GoogleProvider([
            'clientId'     => $clientId,
            'clientSecret' => $clientSecret,
            'redirectUri'  => $redirectUri // will use this same url 'index'
        ]);

        $authUrl = $provider->getAuthorizationUrl([
            'scope' => [
                'https://www.googleapis.com/auth/cloud-platform'
            ]
        ]);

        // This is the first request for authorization
        if (empty($_GET['code']))
        {
            session()->put('oauth2state', $provider->getState());
            return redirect($authUrl);
        }
        // This is the returned request from Google
        else
        {
            $token = $provider->getAccessToken('authorization_code', [
                'code' => $_GET['code']
            ]);

            // I HAVE A TOKEN NOW! I can do a request using Guzzle, like below, and it works fine.
            // But surely Google's package should allow me to do this?!?!?

            $response = Http::get('https://cloudresourcemanager.googleapis.com/v1/projects', [
                'access_token' => $token
            ]);
            return $response;
        }
    }
}
然而,我想利用谷歌已经建立的图书馆的力量

查看googleauth包for PHP(),我看到很多关于OAuth的内容,而且似乎可以将该库生成的凭据与googlecloud包()一起使用,如上面的BillingClient。但我正在努力解决这个问题

编辑


更进一步:在jdp下面的回答中,他使用了'credentials'配置键。请注意CloudBillingGapicClient源代码中的注释:“此外,此选项还可以接受预构造的\Google\Auth\FetchAuthTokenInterface对象或\Google\ApiCore\CredentialsWrapper}对象”。当我查看Google的Auth包中的OAuth2类时,我看到它实现了FetchAuthTokenInterface。因此,它似乎可以用来传递OAuth凭据。我不知道如何创建。

您可以创建一个服务帐户并下载凭据文件,然后进行配置。从那里,客户端将自动进行身份验证

您还可以在创建客户端时直接提供凭据:

$client=新的CloudBillingClient([
“凭证”=>“/path/to/keyfile.json”
]);
$accounts=$client->listBillingAccounts();
foreach($accounts作为$account){
打印('Billing account:'.$account->getName().PHP\u EOL);
}

谢谢您的评论。然而,我需要能够处理许多用户的谷歌云帐户,这将需要他们每个人下载他们的密钥文件并上传到我的服务器上,对吗?我想要一种更加用户友好的身份验证方法,即OAuth方法。我可以确认我有自己的代码。你想在很多用户身上工作是什么意思?是否要列出一个项目的帐单帐户?一个用户拥有的多个项目的名称?或者多个项目有不同的用户?我的web应用有很多用户。每个人都可以通过OAuth2将他们的Google帐户连接到我的web应用程序,然后通过我的应用程序查询他们的项目和账单。因此,对于我的每个应用程序用户,他们将授予我的应用程序访问其谷歌帐户的权限,我将从谷歌获得一个访问令牌,然后可以从谷歌请求有关该用户项目和计费帐户的数据。这就是我的自定义代码在上面所做的;我只想使用谷歌自己的API包来实现这一点。如果有帮助的话,我还为上面的问题添加了一些细节。据我所知,您在项目中创建了应用程序凭据。您只能获得项目的访问令牌,无法访问我的项目。访问我的项目的唯一方法是创建一个服务帐户,我应该在具有wright权限的情况下将此服务帐户添加到我的项目中。否则,您无法访问我的项目。