DFA报告:无法使用服务帐户获取配置文件

时间:2014-07-31 17:07:07

标签: oauth-2.0 google-api google-oauth google-dfp google-oauth-java-client

我正在开发一段代码,可以使用Google DFA Reporting API创建和下载报告。

我可以使用使用Install-App(本机应用程序)帐户生成的客户端ID和客户端密钥来执行相同操作,但是使用此类帐户,它始终首次打开浏览器,然后才会进行身份验证未来的要求。

在进一步阅读时,我遇到了服务帐户。然后我创建了一个新的服务帐户并下载了p12密钥。我现在可以在p12和电子邮件帐户(*******ccms@developer.gserviceaccount.com)的帮助下构建凭证对象。我可以在调用credentials.refreshToken()后在凭证对象中看到访问令牌时确认这一点。

然后我使用上述凭据创建DFA报告对象并尝试获取配置文件列表,但我收到以下错误:

java.lang.IllegalArgumentException: No profiles found
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:92)
    at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:49)
    at com.google.api.services.samples.dfareporting.cmdline.GetAllUserProfiles.list(GetAllUserProfiles.java:52)
    at com.google.api.services.samples.dfareporting.cmdline.DfaReportingSample.main(DfaReportingSample.java:171)

请参阅下面的代码,告诉我出错的地方:

private static Credential authorize() throws Exception {
    // load client secrets

    List<String> SCOPES = ImmutableList
            .of("https://www.googleapis.com/auth/dfareporting");
    String SERVICE_ACCOUNT_EMAIL = "*************************@developer.gserviceaccount.com";

    String Path = DfaReportingSample.class.getResource(
            "/TestDFA-5d985ff38b34.p12").getPath();
    java.io.File file = new java.io.File(Path);

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    Credential credentials =  new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(SCOPES)
            .setServiceAccountPrivateKeyFromP12File(file).build();

    credentials.refreshToken();

    return credentials;
}

private static Dfareporting initializeDfareporting() throws Exception {
    Credential credential = authorize();

    // Create DFA Reporting client.
    return new Dfareporting(httpTransport, JSON_FACTORY, credential);
}


public static void main(String[] args) {

    try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        Dfareporting reporting = initializeDfareporting();

        UserProfiles up = reporting.userProfiles();
        List l = up.list();
        UserProfileList profiles = l.execute();
        // {"etag":"\"bM2H6qONz9kIDiByk_eTdC6Ehcc/vyGp6PvFo4RvsFtPoIWeCReyIC8\"","items":[],"kind":"dfareporting#userProfileList"}
        Preconditions.checkArgument(
            profiles.getItems() != null && !profiles.getItems().isEmpty(), "No profiles found");
        for (UserProfile userProfile : profiles.getItems()) {
          System.out.printf("User profile with ID \"%s\" and name \"%s\" was found.%n",
              userProfile.getProfileId(), userProfile.getUserName());
        }
....................................

PS:如果我使用通过本机应用程序(已安装的应用程序)帐户提供的客户端ID和客户端密码生成的访问令牌,我可以获取所有配置文件。

谢谢, Hussain Bohra

1 个答案:

答案 0 :(得分:1)

在尝试使用服务帐户(&amp; p12键)之后,我们无法使用Google DFA报告。因此,我们现在使用以下类进行身份验证:

com.google.api.ads.common.lib.auth.OfflineCredentials

input = new FileInputStream(Utils.getProperty(Constants.HOME_PATH)
        + Utils.getProperty(Constants.CLIENT_SECRETS_DIRECTORY)
        + Constants.PATH_SEPARATOR + userPropertyFile);
userProperty.load(input);

credential = new OfflineCredentials.Builder()
        .forApi(OfflineCredentials.Api.DFA)
        .withHttpTransport(
                GoogleNetHttpTransport.newTrustedTransport())
        .withClientSecrets(userProperty.getProperty("client_id"),
                userProperty.getProperty("client_secret"))
        .withRefreshToken(userProperty.getProperty("refresh_token"))
        .build().generateCredential();

上述代码中使用的刷新令牌是为每个帐户手动生成的(一次性过程)。使用此凭证对象,我们现在可以在DFA报告中执行以下所有活动:   - 列出配置文件   - 验证和创建报告   - 下载报告数据。

获取刷新令牌的代码(包括在浏览器中单击“接受”的手动步骤)

java.io.File DATA_STORE_DIR = new java.io.File(
        "d:/dfa-reporting/.store/dfareporting_sample");
FileDataStoreFactory dataStoreFactory;
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
        JSON_FACTORY,
        new InputStreamReader(DfaReportingSample.class
                .getResourceAsStream("/client_secret_galtieri.json")));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
        .setDataStoreFactory(dataStoreFactory).build();
// authorize
String refreshToken = new AuthorizationCodeInstalledApp(flow,
        new PromptReceiver()).authorize("hbohra").getRefreshToken();

谢谢, Hussain Bohra