【发布时间】:2014-09-23 18:08:34
【问题描述】:
我正在开发一段代码,它可以使用 Google DFA 报告 API 创建和下载报告。
我可以使用使用 Install-App(本机应用程序)帐户生成的 Client Id 和 Client Secret 来做同样的事情,但是对于这种类型的帐户,它总是第一次打开浏览器,然后才进行身份验证未来的请求。
在进一步阅读时,我发现了服务帐户。然后我创建了一个新的服务帐户并下载了 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 和客户端密码生成的访问令牌,我能够获取所有配置文件。
谢谢, 侯赛因·博拉
【问题讨论】:
标签: oauth-2.0 google-api google-oauth google-dfp google-oauth-java-client