使用 ASP.NET MVC 访问 Google AdWords API v201506

Get access to Google AdWords API v201506 with ASP.NET MVC

我需要使用 AdWrods Api v201506 进行一些简单的操作,但仍然出现授权错误(我将在本 post 的底部传递)。我认为问题不在代码中(我只是从谷歌文档中复制粘贴),而是在帐户设置中,所以找出错误的最佳方法是向您展示我在每一步中所做的事情。

  1. 创建 production MCC 帐户 here (production-mcc@gmail.com)。请注意,我选择了“管理其他人的帐户。”作为“您主要如何使用此 AdWords 经理帐户?”的答案。
  2. 获取 production-mcc@gmail.com 的开发人员令牌。它仍然是 "waiting for approve",但这不是问题,因为我将使用测试帐户。
  3. 创建test MCC 帐户here(用于管理其他人的帐户。,test-mcc@gmail.com).
  4. 在控制台中创建新项目Google Developer as test-mcc@gmail.com (MyProject)。
  5. 在同意屏幕中创建应用程序 (MyProject_App)。
  6. Web 应用程序创建新的客户端 ID:
    • JavaScript 来源:http://localhost:50194/
    • 重定向 URI:http://localhost:50194/oauth2callback(自动生成)和 http://localhost:8080/ - 没有这个 url 我无法使用 OAuthTokenGenerator.exe(来自 here). Installed application as an "Other" type and generate refresh token with OAuthTokenGenerator.exe (from here).
  7. 转到 Google Adwords 并添加客户帐户(按钮 +帐户 -> 创建新的 AdWords 帐户)。保存更改并复制他的 ID。
  8. 现在获取Client ID、Client Secret并成功生成刷新令牌。
  9. 在 MVC 项目中使用 NuGet Google.AdWords 和 Google.Ads.Common 包安装。
  10. 编辑 web.config 文件中的 <AdWordsApi> 部分,如下所示:

    <!-- Settings related to SOAP logging. -->
    <add key="MaskCredentials" value="true" />
    
    <!-- Settings related to general library behaviour. -->
    
    <!-- Use this key to automatically retry a call that failed due to a
         recoverable error like expired credentials. -->
    <!-- <add key="RetryCount" value="1"/> -->
    
    <!-- Set the service timeout in milliseconds. -->
    <!-- <add key="Timeout" value="100000"/> -->
    
    <!-- Use this key to enable or disable gzip compression in SOAP requests.-->
    <add key="EnableGzipCompression" value="true" />
    
    <!-- Proxy settings for library. -->
    <add key="ProxyServer" value="" />
    <add key="ProxyUser" value="" />
    <add key="ProxyPassword" value="" />
    <add key="ProxyDomain" value="" />
    
    <!-- Settings specific to AdWords API.-->
    <add key="UserAgent" value="RANDOM_STRING" />
    <add key="DeveloperToken" value="DEV_TOKEN_FROM_2_PARAGRAPH" />
    
    <!-- If your application is a simple script that makes calls to only a
         single Adwords account, then you can set your customer ID here. If you
         have multiple customer IDs to deal with in your account, then you can
         comment out this key and set the value at runtime by setting
         ((AdWordsAppConfig) user.Config).ClientCustomerId = "xxx";
    -->
    <add key="ClientCustomerId" value="CLIENT_ID_FROM_7_PARAGRAPH" />
    
    <!-- Use the following settings to skip the report header and summary rows
         when downloading a report in CSV, TSV or their gzipped formats. -->
    <add key="SkipReportHeader" value="false" />
    <add key="SkipReportSummary" value="false" />
    <add key="SkipColumnHeader" value="false" />
    
    <!-- Use the following setting to include zero impression rows when
         downloading a report. If this setting is commented out, then the server
         behaves as explained in
         https://developers.google.com/adwords/api/docs/guides/zero-impression-reports#default_behavior.
    -->
    <!-- <add key="IncludeZeroImpressions" value="true"/> -->
    
    <!-- Settings specific to use OAuth2 as authentication mechanism. You could
         run Common\Util\OAuth2TokenGenerator.cs to generate this section of the
         config file.
    -->
    <!-- Provide the OAuth2 client ID and secret. You can create one from
         https://console.developers.google.com. See
         https://github.com/googleads/googleads-dotnet-lib/wiki/Using-OAuth2
         for more details.
    -->
    <add key="OAuth2ClientId" value="CLIENT_ID_FROM_8_PARAGRAPH" />
    <add key="OAuth2ClientSecret" value="CLIENT_SECRET_FROM_8_PARAGRAPH" />
    
    <!-- The following OAuth2 settings are optional. -->
    <!-- Provide a different OAuth2 scope if required. Multiple scopes should be
         separated by spaces. -->
    <!-- <add key="OAuth2Scope" value="INSERT_OAUTH2_SCOPE_HERE" /> -->
    
    <!-- Use the following keys if you want to use Web / Installed application
         OAuth flow.-->
    
    <add key="OAuth2Mode" value="APPLICATION" />
    <!-- If you are using a single MCC account's credentials to make calls to
         all your accounts, then you can run OAuth2TokenGenerator.cs to generate
         a RefreshToken for that account and set this key in your application's
         App.config / Web.config. If you are making calls to multiple unrelated
         accounts, then you need to implement OAuth2 flow in your account and
         set this key at runtime. See OAuth folder under Examples folder for a
         web and a console application example.
    -->
    <add key="OAuth2RefreshToken" value="REFRESH_TOKEN_FROM_8_PARAGARAPH" />
    
    <!-- Optional: Specify an OAuth2 redirect url if you are building a
         web application and implementing OAuth2 web flow in your application.
    -->
    <!-- <add key="OAuth2RedirectUri" value="" /> -->
    
    
    <!-- Use the following keys if you want to use OAuth2 service account flow.
         You should comment out all the keys for Web / Installed application
         OAuth flow above. See
         https://developers.google.com/adwords/api/docs/guides/service-accounts
         https://github.com/googleads/googleads-dotnet-lib/wiki/Using-OAuth2
         for more details.
    -->
    <!--
    <add key="OAuth2Mode" value="SERVICE_ACCOUNT" />
    <add key="OAuth2ServiceAccountEmail"
        value="INSERT_OAUTH2_SERVICE_ACCOUNT_EMAIL_HERE" />
    <add key="OAuth2PrnEmail" value="INSERT_OAUTH2_USER_EMAIL_HERE" />
    <add key="OAuth2JwtCertificatePath"
        value="INSERT_OAUTH2_JWT_CERTIFICATE_PATH_HERE" />
    <add key="OAuth2JwtCertificatePassword"
        value="INSERT_OAUTH2_JWT_CERTIFICATE_PASSWORD_HERE" />
    -->
    

现在,我只是从 Google Developers Documentation 中的示例复制粘贴代码。

在 运行 之后我在以下位置遇到异常:

BudgetReturnValue budgetRetval = budgetService.mutate(
    new BudgetOperation[] {budgetOperation});

它调用:[AuthorizationError.USER_PERMISSION_DENIED @;触发器:''] 根据 documentation 表示:"User doesn't have permission to access customer."。 我的问题是:我在授权方面做错了什么?哪个令牌或 ID 无效?

PS。我描述了我完成的所有步骤 - 不多不少。

我找到了异常的解决方案([AuthorizationError.USER_PERMISSION_DENIED @ ; trigger:'<null>']):问题出在刷新令牌中——当我生成他的令牌时,我是在我的私人帐户中登录的,而不是作为 test-mcc@gmail.com。因此,当您使用 OAuthTokenGenerator.exe 时,请确保在生成器显示新 window 并请求许可时登录正确的帐户。