【发布时间】: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 访问令牌。如何解决这个问题,我错过了什么?
【问题讨论】:
-
您需要获得 Oauth2 的授权才能访问该方法。它在文档中究竟在哪里说明了这个 api 支持服务帐户?你能给我们一个你正在关注的文档的链接吗?
-
@DalmTo 基于此 github.com/googleads/google-ads-dotnet 。此存储库在此处作为示例链接developers.google.com/google-ads/api/docs/client-libs
-
从我可以看到这里他们只有安装的应用程序和 Web 应用程序示例是什么让您认为支持服务帐户? github.com/googleads/google-ads-dotnet/tree/master/examples/…
-
@DalmTo strang,链接在我的手机上有效,在我的电脑上无效。无论如何,github.com/googleads/google-ads-dotnet/blob/master/src/… 非常清楚地说明了使用服务帐户的可能性
标签: c# google-api google-ads-api