2013-11-01 19 views
2

我正在使用Google Adwords API升級我們的代碼,以便從v201302遷移到v201309。 任何人都可以建議我,我們應該使用什麼代碼來代替下面的代碼(因爲ClientLoginToken現在已被棄用)。ClientLoginTokens的替代方案

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

回答

4

以下是我用來獲取OAuth2的步驟。當然情況因人而異...

第1步 - 使用您的電子郵件和密碼以上

  • 團長Google API Console與谷歌API控制檯應用程序註冊

    1. 登錄到谷歌。您可能被重定向到Google Cloud Console
    2. 在'API &驗證'下點擊'同意屏幕'。至少填寫'產品名稱'和'電子郵件'。
    3. 在'APIs &驗證'下點擊'已註冊的應用程序'。
    4. 點擊「註冊應用程序」。填寫詳細信息,確保您選擇「Native」作爲平臺。
    5. 在'OAuth 2.0客戶端ID'下面記錄了CLIENT ID和CLIENT SECRET值。

    步驟2 - 生成刷新令牌

    下一步是生成一個刷新令牌。這是生成一次性使用多次令牌,允許您的應用程序獲得新的訪問令牌:

    1. 下載GetRefreshToken.java
    2. 創建一個aps.properties文件,由GoogleClientSecretsBuilder() .forApi(Api.ADWORDS)調用引用。這ads.properties文件應包含兩行:

      api.adwords.clientId =客戶端ID - 從 - step1.6

      api.adwords.clientSecret =客戶的祕密 - 從 - step1.6

    3. 使用網絡瀏覽器登錄到Google AdWords MCC。

    4. 運行GetRefreshToken.java,並按照指示,即一份瀏覽器的URL到瀏覽器中,輸入代碼返回到控制檯等等,等等
    5. 您現在應該有一個refreshToken。將此刷新標記複製到您的ads.properties文件中,如下所示:

    api.adwords。refreshToken = your-refresh-token

    PS GetRefreshToken.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,客戶端ID & clientSecret在ads.properties你現在可以產生憑證像這樣:

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

    第4步 - 獲取的AdWords會話

    最後一步(如果您有這麼多的話,最好的辦法是使用oAuth2CredentialCredential實例創建一個AdWords會話)。您可以通過在廣告中再添加兩件事來做到這一點屬性文件:

    api.adwords.developerToken =開發人員令牌從-MCC

    api.adwords.clientCustomerId =客戶ID-的-的AdWords帳戶,也就是說,您-want-訪問

    然後得到一個AdWor DS會話使用起來像這樣:

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

    第5步 - 買一杯咖啡和它是多麼容易訪問使用的OAuth2

    這一步完全是可選的谷歌AdWords API的反映。

  • 1

    您不能像以前一樣轉換舊的過程。 Google的Migration Guide中有一些示例。請參閱身份驗證/ 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。文章Using OAuth 2.0 for Login中詳細介紹了Google的OAuth 2.0。

    +0

    謝謝Dag ..我應該用什麼來取代客戶端ID和secret.can,我們使用adword客戶端ID代替客戶端ID。 – Sheo