Reputation: 415
I really looked it up and found no suitable answer.
I have both an AdSense and AdMob accounts sharing the same publisher ID. They're active and when I try the API in https://developers.google.com/adsense/management/v1.4/reference/accounts/list it works fine.
1) Created an AdSense Account with my personal email.
2) Enabled the AdSense Management API in https://console.developers.google.com/apis/credentials
3) Created a Service Account and downloaded the credential's secrets json file
5) Waited at least 48 hours. Probably more, now.
4) My pom.xml file has these dependencies:
<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) My code
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) Error message
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."
}
Am I missing something? I've followed every stackoverflow entry I've found about this subject to no avail.
When I debug, the credential object looks ok, with all the service account secrets loaded. It gets an AuthToken when I do the credential.refreshToken(); command. I don't understand what's missing.
Any help would be much appreciated.
Upvotes: 2
Views: 1537
Reputation: 109
from https://developers.google.com/adsense/management/getting_started "Service Accounts are not supported ..."
If you follow the getting stared guide/ examples and turn on offline access you can generate (and store) a re-usable token that can be used to make future calls.
If you are using the java example at https://github.com/googleads/googleads-adsense-examples/blob/master/java/v1.x/src/main/java/com/google/api/services/samples/adsense/cmdline/AdSenseSample.java take a look in DATA_STORE_DIR
Upvotes: 0