【问题标题】:Google API Oauth2: Only one refresh token for connected accountsGoogle API Oauth2:连接帐户只有一个刷新令牌
【发布时间】: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


【解决方案1】:

通过阅读来自 Google 的文档和许多论坛帖子,我发现的唯一解决方案是,当有人希望通过一个经理帐户与两个或多个不同的 Google Ads 帐户建立连接时,复制 refresh_token。

我的解决方案: - 通过将经理帐户的电子邮件保存为单独的值来连接到第一个帐户 - 将此条目保存在数据库中作为活动连接 - 通过再次保存经理帐户的电子邮件连接到第二个帐户。在这种情况下,Google 不会提供 refresh_token,因为应用程序已经通过第一个连接进行连接。这就是为什么我从第一个连接的条目中获取 refresh_token 的原因。对我来说很好用!

如果您采用此解决方案,请在断开连接时不要忘记检查此经理帐户的可用连接。因为你可以断开所有的连接。

【讨论】:

    猜你喜欢
    • 2016-08-03
    • 1970-01-01
    • 2013-08-30
    • 2016-10-13
    • 1970-01-01
    • 2020-02-21
    • 2021-08-27
    • 2022-06-19
    • 1970-01-01
    相关资源
    最近更新 更多