문제

I am working on google Adwords API to upgrade our code for migration from v201302 to v201309. Any one can suggest me, what code we should use in place of following code ( as ClientLoginTokens now deprecated).

String clientLoginToken = new ClientLoginTokens.Builder()
                    .forApi(ClientLoginTokens.Api.ADWORDS)
                    .withEmailAndPassword(configurations.get("email"), configurations.get("password"))
                    .build()
                    .requestToken();
도움이 되었습니까?

해결책

Here are the steps that I took to get OAuth2 working. YMMV of course...

Step 1 - Register Application with Google Console API

  1. Log into Google using your email and password above
  2. Head to the Google API Console. You probably get redirected to Google Cloud Console
  3. Under 'APIs & Auth' click on 'Consent screen'. Fill in at least 'Product Name' and 'Email'.
  4. Under 'APIs & Auth' click on 'Registered apps'.
  5. Click 'Register App'. Fill in details ensuring that you select 'Native' as platform.
  6. Under 'OAuth 2.0 Client ID' make a note of the CLIENT ID and CLIENT SECRET values.

Step 2 - Generate Refresh Token

Next step is to generate a refresh token. This is a generate-once-use-multiple-times token that allows your application to obtain new access tokens:

  1. Download GetRefreshToken.java.
  2. Create an aps.properties file to be referenced by the GoogleClientSecretsBuilder() .forApi(Api.ADWORDS) call. This ads.properties file should contain two lines:

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

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

  3. Using web browser log into the Google AdWords MCC.

  4. Run GetRefreshToken.java and follow instructions i.e. copy browser URL into browser, enter code returned into console etc. etc.
  5. You should now have a refreshToken. Copy this refresh token into your ads.properties files like this:

api.adwords.refreshToken=your-refresh-token

PS GetRefreshToken.java has a couple of dependencies. If you are using Maven then here they are (adjust versions accordingly!):

    <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>

Step 3 - Generate Credential

With your refreshToken, clientId & clientSecret in your ads.properties you can now generate a Credential like this:

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

Step 4 - Get AdWords Session

The final step (hats off to you if you have got this far!) is to create an AdWords Session using the oAuth2Credential instance of Credential that you created in Step 3. You can do this by adding two more things into your ads.properties file:

api.adwords.developerToken=developer-token-from-mcc

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

Then get an AdWords session up using like this:

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

Step 5 - Grab a coffee and reflect on how easy it is to access the Google AdWords API using OAuth2

This step is entirely optional.

다른 팁

You can not transform your old process identical as before. There are some examples in the Migration Guide from Google. See the Authentication/OAuth 2.0 section:

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. 

You might change Api.DFP to Api.ADWORDS. OAuth 2.0 at Google is fully covered in the Using OAuth 2.0 for Login article.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top