Google广告API PHP

发布于 2025-02-10 10:03:19 字数 1091 浏览 0 评论 0原文

我正在尝试使用服务帐户与Google ADS API连接。

在Momet上,我的代码看起来像这样:

$oAuth2Credential = (new OAuth2TokenBuilder())
        ->withJsonKeyFilePath(self::CREDENTIALS_FILE_PATH)
        ->withScopes('https://www.googleapis.com/auth/adwords')
        ->withImpersonatedEmail(IMPERSONATED_EMAIL)
        ->build();

    $googleAdsClient = (new GoogleAdsClientBuilder())
        ->withOAuth2Credential($oAuth2Credential)
        ->withDeveloperToken(DEVELOPER_TOKEN)
        ->build();
    $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();

    $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
    $stream =
        $googleAdsServiceClient->searchStream(CUSTOMER_ID, $query);

在Google Cloud中,我创建了一个项目和一个服务帐户。该帐户与我可以在Google广告中访问的帐户相同。另外,我还在Google Workspace设置了整个领域的代表团,并拥有相同的帐户。我认为我做对了,但是我遇到了这个错误:

“在此处输入图像说明”

我希望有人可以帮助我。

I am trying to use a service account to connect with the Google Ads API.

At the momet my code looks like this:

$oAuth2Credential = (new OAuth2TokenBuilder())
        ->withJsonKeyFilePath(self::CREDENTIALS_FILE_PATH)
        ->withScopes('https://www.googleapis.com/auth/adwords')
        ->withImpersonatedEmail(IMPERSONATED_EMAIL)
        ->build();

    $googleAdsClient = (new GoogleAdsClientBuilder())
        ->withOAuth2Credential($oAuth2Credential)
        ->withDeveloperToken(DEVELOPER_TOKEN)
        ->build();
    $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();

    $query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
    $stream =
        $googleAdsServiceClient->searchStream(CUSTOMER_ID, $query);

In Google cloud I have created a project and a service account. The account is the same I have access to at Google Ads. Also I have set the Domain-Wide Delegation at Google Workspace, with this same account. In my opinion I did everything right, but I am getting this error:

enter image description here

I hope there's someone who can help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2025-02-17 10:03:19

HY JOL123,Google中的服务帐户更像是普通的用户帐户。因此,要使普通用户访问您的广告帐户,您必须与用户共享对该帐户的访问权限(使用用户的电子邮件地址)。

服务帐户必须做同样的事情。您必须进入您希望访问和共享访问权限的Google Ads帐户的服务帐户“电子邮件”。

Hy jol123, Service accounts in Google are more like normal user accounts. So for a normal user to access your Ads account, you would have to share access to that account with the user(using the user's email address).

The same thing has to be done for the service account. You have to go into the Google ads account you wish to access and share access with the service account "email" you are using.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文