我真的查了一下,发现没有合适的答案。
我同时拥有AdSense和AdMob帐户共享相同的发布商ID。它们是活跃的,当我在https://developers.google.com/adsense/management/v1.4/reference/accounts/list中尝试API时,它运行正常。
1)使用我的个人电子邮件创建了一个AdSense帐户
2)在https://console.developers.google.com/apis/credentials中启用了AdSense Management API
3)创建了一个服务帐户并下载了凭证的秘密json文件
5)等了至少48小时。可能更多,现在。
4)我的pom.xml文件具有以下依赖项:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-adsense</artifactId>
<version>v1.4-rev160-1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.21.0</version>
</dependency>
5)我的代码
private static Credential authorize() throws Exception {
FileInputStream is = new FileInputStream( "/data/admob-test.json" );
GoogleCredential credential = GoogleCredential.fromStream( is ).createScoped( AdSenseScopes.all() );
credential.refreshToken();
return credential;
}
private static AdSense initializeAdsense() throws Exception {
// Authorization.
Credential credential = authorize();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// Set up AdSense Management API client.
AdSense adsense = new AdSense.Builder( httpTransport, jsonFactory, credential ).setApplicationName( "random-app-name" ).build();
return adsense;
}
public static void main( String[] args ) {
try {
AdSense adsense = initializeAdsense();
// I get an exception on the following line:
Accounts accounts = adsense.accounts().list().execute();
if ( ( accounts.getItems() != null ) && !accounts.getItems().isEmpty() ) {
System.out.println( "SUCCESS!" );
}
else {
System.out.println( "FAIL!" );
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
6)错误信息
403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "User does not have an AdSense account.",
"reason" : "noAdSenseAccount"
} ],
"message" : "User does not have an AdSense account."
}
我错过了什么吗?我已经跟踪了我发现的关于这个主题的每个stackoverflow条目都无济于事。
当我调试时,凭证对象看起来没问题,加载了所有服务帐户机密。当我执行credential.refreshToken()时,它会获得一个AuthToken;命令。我不明白缺少什么。
非常感谢任何帮助。
答案 0 :(得分:0)
“不支持服务帐户......”
如果您按照获取的指南/示例并打开离线访问权限,您可以生成(并存储)可用于进行未来呼叫的可重用令牌。
如果您在https://github.com/googleads/googleads-adsense-examples/blob/master/java/v1.x/src/main/java/com/google/api/services/samples/adsense/cmdline/AdSenseSample.java使用java示例,请查看DATA_STORE_DIR