Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
// Copyright 2013 Google Inc. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
package adwords.axis.auth;
16
 
17
import com.google.api.ads.common.lib.auth.GoogleClientSecretsBuilder;
18
import com.google.api.ads.common.lib.auth.GoogleClientSecretsBuilder.Api;
19
import com.google.api.ads.common.lib.exception.ValidationException;
20
import com.google.api.client.auth.oauth2.Credential;
21
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
22
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
23
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
24
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
25
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
26
import com.google.api.client.http.javanet.NetHttpTransport;
27
import com.google.api.client.json.jackson2.JacksonFactory;
28
import com.google.common.collect.Lists;
29
 
30
import java.io.BufferedReader;
31
import java.io.InputStreamReader;
32
 
33
/**
34
 * This example will create an OAuth2 refresh token that can be used with the
35
 * OfflineCredentials utility. Please copy the refresh token into your
36
 * ads.properites file after running.
37
 *
38
 * This example is meant to be run from the command line and requires user
39
 * input.
40
 *
41
 * Credentials and properties in {@code fromFile()} are pulled from the
42
 * "ads.properties" file. See README for more info.
43
 *
44
 * @author Adam Rogal
45
 */
46
public class GetRefreshToken {
47
 
48
  private static final String SCOPE = "https://adwords.google.com/api/adwords";
49
 
50
  // This callback URL will allow you to copy the token from the success screen.
51
  private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
52
 
53
  private static Credential getOAuth2Credential(GoogleClientSecrets clientSecrets)
54
      throws Exception {
55
    GoogleAuthorizationCodeFlow authorizationFlow = new GoogleAuthorizationCodeFlow.Builder(
56
        new NetHttpTransport(),
57
        new JacksonFactory(),
58
        clientSecrets,
59
        Lists.newArrayList(SCOPE))
60
        // Set the access type to offline so that the token can be refreshed.
61
        // By default, the library will automatically refresh tokens when it
62
        // can, but this can be turned off by setting
63
        // api.adwords.refreshOAuth2Token=false in your ads.properties file.
64
        .setAccessType("offline").build();
65
 
66
    String authorizeUrl =
67
        authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build();
68
    System.out.println("Paste this url in your browser: \n" + authorizeUrl + '\n');
69
 
70
    // Wait for the authorization code.
71
    System.out.println("Type the code you received here: ");
72
    String authorizationCode = new BufferedReader(new InputStreamReader(System.in)).readLine();
73
 
74
    // Authorize the OAuth2 token.
75
    GoogleAuthorizationCodeTokenRequest tokenRequest =
76
        authorizationFlow.newTokenRequest(authorizationCode);
77
    tokenRequest.setRedirectUri(CALLBACK_URL);
78
    GoogleTokenResponse tokenResponse = tokenRequest.execute();
79
 
80
    // Create the OAuth2 credential.
81
    GoogleCredential credential = new GoogleCredential.Builder()
82
        .setTransport(new NetHttpTransport())
83
        .setJsonFactory(new JacksonFactory())
84
        .setClientSecrets(clientSecrets)
85
        .build();
86
 
87
    // Set authorized credentials.
88
    credential.setFromTokenResponse(tokenResponse);
89
 
90
    return credential;
91
  }
92
 
93
  public static void main(String[] args) throws Exception {
94
    // Get the client ID and secret from the ads.properties file.
95
    // If you do not have a client ID or secret, please create one in the
96
    // API console: https://code.google.com/apis/console#access and set it
97
    // in the ads.properties file.
98
    GoogleClientSecrets clientSecrets = null;
99
    try {
100
      clientSecrets = new GoogleClientSecretsBuilder()
101
          .forApi(Api.ADWORDS)
102
          .fromFile()
103
          .build();
104
    } catch (ValidationException e) {
105
      System.err.println(
106
          "Please input your client ID and secret into your ads.properties file, which is either "
107
          + "located in your home directory in your src/main/resources directory, or "
108
          + "on your classpath. If you do not have a client ID or secret, please create one in "
109
          + "the API console: https://code.google.com/apis/console#access");
110
      System.exit(1);
111
    }
112
 
113
    // Get the OAuth2 credential.
114
    Credential credential = getOAuth2Credential(clientSecrets);
115
 
116
    System.out.printf("Your refresh token is: %s\n", credential.getRefreshToken());
117
 
118
    // Enter the refresh token into your ads.properties file.
119
    System.out.printf("In your ads.properties file, modify:\n\napi.adwords.refreshToken=%s\n",
120
        credential.getRefreshToken());
121
  }
122
}