Google Ads API -如何使用PHP获取访问和引用令牌

hi3rlvi2  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(292)

我正在关注此软件包:https://github.com/googleads/google-ads-php .在这里他们说我需要运行这个页面:
https://github.com/googleads/google-ads-php/blob/main/examples/Authentication/GenerateUserCredentials.php
到终端。
我就这样做了:

php /locaation of the fie/GenerateUserCredentials.php

之后,我可以得到一个链接,它是这样的:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=535777736006-d1rp8msevnls2pmihk7b8l23j3vra7duh.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fcallback&state=4e23ce963534701029c1a22d2de7848d034d8b0b0&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords
(以上链接为演示目的)
所以,当我点击这个链接,我重定向到我的谷歌帐户,并给予访问他们,之后我重定向回到这个下面的URL:
请输入您的密码,然后点击这里返回上一页。请输入您的密码。
在这里,您可以看到我有两个参数,分别是statecode

现在我的问题是如何使用此URL获取访问令牌和刷新令牌?

e4eetjau

e4eetjau1#

在这里,您可以看到我有两个参数,分别是statecode
下面是一个来自google-ads-php源代码的例子(打开链接查看完整的例子/代码):

// Exit if the state is invalid to prevent request forgery.
$state = $request->getQueryParams()['state'];
if (empty($state) || ($state !== $oauth2->getState())) {
    throw new UnexpectedValueException(
        "The state is empty or doesn't match expected one." . PHP_EOL
    );
};

// Set the authorization code and fetch refresh and access tokens.
$code = $request->getQueryParams()['code'];
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();

$refreshToken = $authToken['refresh_token'];
print 'Your refresh token is: ' . $refreshToken . PHP_EOL;

相关问题