【问题标题】:How to fix google ads api unathorized error when using service account使用服务帐户时如何修复 google ads api 未经授权的错误
【发布时间】:2020-10-29 12:11:49
【问题描述】:

我正在执行 azure 函数,它应该定期从 Google Ads API 获取广告报告并将其保存为 CSV。 从谷歌文档复制代码给我留下了这个

public static void Run([TimerTrigger("0 22 12 * * *")] TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        RunRequest(new GoogleAdsClient(new GoogleAdsConfig() { 
            DeveloperToken = "/*token*/",
            OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,
            OAuth2PrnEmail = "/*service account email*/",
            OAuth2SecretsJsonPath = "/*service account json*/"
        }), "/*client id*/", log);
    }

public static void RunRequest(GoogleAdsClient client, string customerId, ILogger log)
    {
        // Get the GoogleAdsService.
        GoogleAdsServiceClient googleAdsService = client.GetService(
            Services.V5.GoogleAdsService);

        // Create the query.
        string query = @"/*request*/";

        try
        {
            // Issue a search request.
            googleAdsService.SearchStream(customerId, query,
                delegate (SearchGoogleAdsStreamResponse resp)
                {
                    using var writer = new StreamWriter($"reports\\report{DateTime.Now}.csv");
                    using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
                    csv.WriteRecords(Report.BuildReports(resp.Results));
                }
            );
        }
        catch (GoogleAdsException e)
        {
            log.LogInformation("Failure:");
            log.LogInformation($"Message: {e.Message}");
            log.LogInformation($"Failure: {e.Failure}");
            log.LogInformation($"Request ID: {e.RequestId}");
            throw;
        }
    }

执行这段代码给了我这个内容的异常:

"Status(StatusCode="Uauthenticated", Detail="Request is missing required authentication credential。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。请参阅 https://developers.google.com/identity/sign-in/web/devconsole-project."

据我了解,我在使用服务帐户时不需要 OAuth2 访问令牌。如何解决这个问题,我错过了什么?

【问题讨论】:

标签: c# google-api google-ads-api


【解决方案1】:

如参考文档的authorization section 中所述,只有拥有 G Suite(现称为 Google Workspace)域才能使用服务帐号。

您需要为给定的 OAuth 客户端设置 domain-wide delegation,然后为有权访问您需要使用的 Google Ads 帐户的 G Suite 帐户设置 user impersonation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 2016-06-25
    • 2017-06-13
    • 2013-08-21
    • 2018-10-20
    • 1970-01-01
    • 2017-02-22
    • 2018-09-03
    相关资源
    最近更新 更多