基于javagoogleapi的服务访问

基于javagoogleapi的服务访问,java,google-api,google-oauth,Java,Google Api,Google Oauth,我正在尝试使用JavaGoogleAPI访问Adsense管理API。但是,我发现oauth2身份验证有问题。根据以下代码应足够: public static GoogleCredential getCredential() throws GeneralSecurityException, IOException { File file = new File("key.p12"); return new GoogleCredential.Builder().setTran

我正在尝试使用JavaGoogleAPI访问Adsense管理API。但是,我发现oauth2身份验证有问题。根据以下代码应足够:

public static GoogleCredential getCredential() 
   throws GeneralSecurityException, IOException {
    File file = new File("key.p12");
    return new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(SERVICE_ACCOUNT_ID)
            .setServiceAccountScopes(AdSenseScopes.ADSENSE)
            .setServiceAccountPrivateKeyFromP12File(file)
            .setServiceAccountUser(ACCOUNT_USER)
            .build();
}
但是,凭据始终返回accessToken null,我无法执行任何操作。我收到一个错误的请求:

Exception in thread "main" java.lang.RuntimeException:   com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
我的客户代码:

private static AdSense initializeAdsense() throws Exception {
    // Authorization.
    Credential credential = getCredential();        
    String token = credential.getAccessToken();
    System.out.println(token);
    AdSense adsense = new AdSense.Builder(new NetHttpTransport(), JSON_FACTORY, credential).setApplicationName("Google-AdSenseSample/1.2").build();
    return adsense;
}

public static Accounts run(AdSense adsense, int maxPageSize) throws Exception {
    System.out.println("=================================================================");
    System.out.println("Listing all AdSense accounts");
    System.out.println("=================================================================");

    // Retrieve account list in pages and display data as we receive it.
    String pageToken = null;
    Accounts accounts = null;
    do {
        accounts = adsense.accounts().list().setMaxResults(maxPageSize).setPageToken(pageToken).execute();

        if ((accounts.getItems() != null) && !accounts.getItems().isEmpty()) {
            for (Account account : accounts.getItems()) {
                System.out.printf("Account with ID \"%s\" and name \"%s\" was found.\n", account.getId(), account.getName());
            }
        } else {
            System.out.println("No accounts found.");
        }

        pageToken = accounts.getNextPageToken();
    } while (pageToken != null);

    System.out.println();
    return accounts;
}

public static void test() throws Exception {
    AdSense adsense = initializeAdsense();
    run(adsense, 30);
}
问题是,到底出了什么问题

编辑: 也许我就是做不到

恐怕不支持通过服务帐户身份验证访问AdSense管理API,因为它是受保护的用户信息


服务帐户ID应为您的服务帐户的电子邮件地址xxxxxx@developer.gserviceaccount.com不要设置用户帐户。

也许这是正确的答案,但我已经尝试过了。在本例中,我在主线程java.lang.RuntimeException:com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止{代码:403,错误:[{域:全局,消息:用户没有AdSense帐户,原因:NoadSenseCount}],消息:用户没有AdSense帐户。}位于Test.mainTest.java:28