2

我正在使用 google Adwords API 来升级我们的代码,以便从 v201302 迁移到 v201309。任何人都可以建议我,我们应该使用什么代码代替以下代码(因为现在不推荐使用 ClientLoginTokens)。

String clientLoginToken = new ClientLoginTokens.Builder()
                    .forApi(ClientLoginTokens.Api.ADWORDS)
                    .withEmailAndPassword(configurations.get("email"), configurations.get("password"))
                    .build()
                    .requestToken();
4

2 回答 2

4

以下是我为使 OAuth2 工作而采取的步骤。YMMV 当然...

第 1 步 - 使用 Google Console API 注册应用程序

  1. 使用上面的电子邮件和密码登录 Google
  2. 前往Google API 控制台。您可能会被重定向到 Google Cloud Console
  3. 在“API 和身份验证”下,单击“同意屏幕”。至少填写“产品名称”和“电子邮件”。
  4. 在“APIs & Auth”下点击“Registered apps”。
  5. 点击“注册应用”。填写详细信息,确保选择“本机”作为平台。
  6. 在“OAuth 2.0 Client ID”下,记下 CLIENT ID 和 CLIENT SECRET 值。

第 2 步 - 生成刷新令牌

下一步是生成刷新令牌。这是一个生成一次使用多次的令牌,它允许您的应用程序获取新的访问令牌:

  1. 下载GetRefreshToken.java
  2. 创建一个aps.properties要被GoogleClientSecretsBuilder() .forApi(Api.ADWORDS)调用引用的文件。该ads.properties文件应包含两行:

    api.adwords.clientId=client-id-from-step1.6

    api.adwords.clientSecret=client-secret-from-step1.6

  3. 使用网络浏览器登录 Google AdWords MCC。

  4. 运行GetRefreshToken.java并按照说明操作,即将浏览器 URL 复制到浏览器中,将返回的代码输入控制台等。
  5. 您现在应该有一个 refreshToken。将此刷新令牌复制到您的ads.properties文件中,如下所示:

api.adwords.refreshToken=你的刷新令牌

PSGetRefreshToken.java有几个依赖项。如果您使用的是 Maven,那么它们就在这里(相应地调整版本!):

    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-oauth2</artifactId>
        <version>v2-rev50-1.17.0-rc</version>
    </dependency>

    <dependency>
        <groupId>com.google.api-ads</groupId>
        <artifactId>adwords-axis</artifactId>
        <version>1.20.0</version>
    </dependency>

第 3 步 - 生成凭证

使用您的 refreshToken、clientId 和 clientSecret,您ads.properties现在可以生成这样的凭证:

Credential oAuth2Credential = new OfflineCredentials.Builder()
    .forApi(Api.ADWORDS)
    .fromFile()
    .build()
    .generateCredential();

第 4 步 - 获取 AdWords 会话

最后一步(如果您已经做到了这一点,请向您致敬!)是使用您在第 3 步中创建的oAuth2Credential实例创建一个 AdWords 会话Credential。您可以通过在 ads.properties 文件中添加另外两个内容来完成此操作:

api.adwords.developerToken=开发者令牌来自-mcc

api.adwords.clientCustomerId=客户 id-of-adwords-account-that-you-want-to-access

然后使用如下方式启动 AdWords 会话:

AdWordsSession awSession =
                new AdWordsSession.Builder()
                .fromFile()
                .withOAuth2Credential(oAuth2Credential)
                .build();

第 5 步 - 喝杯咖啡,想想使用 OAuth2 访问 Google AdWords API 是多么容易

此步骤完全是可选的。

于 2013-11-07T11:37:51.003 回答
1

您不能像以前一样转换旧流程。Google的迁移指南中有一些示例。请参阅身份验证/OAuth 2.0 部分:

If you are coming from using ClientLogin, we've added a few features to make it extremely easy to switch over.

Once you've generated a refresh token using the GetRefreshToken.java example of your examples download, and you've copied it into your ads.properties file, you'll be able to create a refreshable token with the OfflineCredentials utility.

Credential oAuth2Credential = new OfflineCredentials.Builder()
.forApi(Api.DFP)
.fromFile()
.build()
.generateCredential();

Once authorized, you can set the Credential object into the builder or session:

DfpSession session = new DfpSession.Builder()
.fromFile()
.withOAuth2Credential(oAuth2Credential)
.build();

OAuth2 will now be used when making API calls. 

您可以将 Api.DFP 更改为 Api.ADWORDS。Google 的 OAuth 2.0 在Using OAuth 2.0 for Login一文中全面介绍。

于 2013-11-05T09:04:44.997 回答