【发布时间】:2020-03-07 00:17:21
【问题描述】:
当我的@gmail.com 帐户对两个不同的 Google Ads 帐户具有读取权限时,我正在使用 Google Oauth2 API 连接到 Google Ads 并仅成为一个刷新令牌。
我有帐户 a@gmail.com。此帐户对 Google Ads 帐户 b@gmail.com 和 c@gmnail.com 具有读取权限。
当我使用电子邮件 a@gmail.com 为帐户 b@gmail.com 和 c@gmail.com 创建两个到 Google Ads 的连接时,我只收到一个用于第一个连接的刷新令牌,而没有收到任何刷新令牌用于第二次连接。为什么?我该如何管理这个,成为每个连接的 2 个刷新令牌? 成为第二次连接的刷新令牌的唯一方法是转到我的帐户 a@gmail.com 并手动拒绝对我的应用程序的访问并再次与 c@gmail.com 连接。但是 b@gmail.com 没有刷新令牌了。
代码:
$oauth2 = new OAuth2(
[
'authorizationUri' => $this->container->getParameter('oauth2.google.adwords.authorizationUri'),
'tokenCredentialUri' => $this->container->getParameter('oauth2.google.adwords.tokenCredentialUri'),
'redirectUri' => $this->container->getParameter('domain.system') . $this->container->getParameter(
'oauth2.google.adwords.redirectUri.advertiser'
),
'clientId' => $this->container->getParameter('oauth2.google.adwords.clientId'),
'clientSecret' => $this->container->getParameter('oauth2.google.adwords.clientSecret'),
'scope' => $this->container->getParameter('oauth2.google.adwords.scope')
]
);
// Create a 'state' token to prevent request forgery.
// Store it in the session for later validation.
$randomState = sha1(openssl_random_pseudo_bytes(1024)) . '---' . $accountId;
$oauth2->setState($randomState);
// Redirect the user to the authorization URL.
$config = [
// Set to 'offline' if you require offline access.
'access_type' => 'offline',
'approval_prompt' => 'force'
];
// redirect to google ads
return new RedirectResponse($oauth2->buildFullAuthorizationUri($config));
【问题讨论】:
-
只有一个刷新令牌是什么意思?每个对您的代码进行身份验证的用户都将获得一个新的访问令牌和一个刷新令牌。请附上您的代码。
-
每个用户都有不同的帐户。我正在为 2 个 Google 广告帐户连接一个帐户。
-
每个拥有不同帐户的用户将返回不同的刷新令牌。因为您使用的是 PHP,所以只有在您首次验证用户身份时才会返回刷新令牌,Google 假定您已保存它。你真的应该考虑使用 google api php 客户端库。
-
如果我使用google api php客户端库有什么区别?我现在正在使用 googleads/googleads-php-lib。
标签: php google-api google-ads-api google-oauth refresh-token