The Google Webmaster API for Java returns an empty list of sites

I wrote a simple site request code that Oauth uses with a documentation- based service account . The authentication key file (.p12) used is valid as well as the account.

The problem is that the node list method returns an empty list.

service.sites().list().execute(); 

Also, if I explicitly try to get Sitemaps for the site I’m checking by calling

 service.sitemaps().list("my.sample.site.com").execute(); 

I got a 403 ban - "The user does not have sufficient permission for the site" sample.site.com. "See also: https://support.google.com/webmasters/answer/2451999 ." API error.

According to my debugging, the API loads the key file (.p12) perfectly and manages access tokens, etc. no problem.

However, there may be a problem with my service account authentication.

Dependencies:

 <dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-webmasters</artifactId> <version>v3-rev6-1.20.0</version> </dependency> 

Code example:

 package webmastertools; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.webmasters.Webmasters; import com.google.api.services.webmasters.WebmastersScopes; import com.google.api.services.webmasters.model.SitesListResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.File; import java.util.Collections; public class GoogleWebmastersClient { static Log logger = LogFactory.getLog(GoogleWebmastersClient.class); public static void main(String args[]) { try { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); String emailAddress = " 012345789@developer.gserviceaccount.com "; String applicationName = "Sitemap Generator"; GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(emailAddress) .setServiceAccountPrivateKeyFromP12File(new File("/path/to/auth.p12")) .setServiceAccountScopes(Collections.singletonList(WebmastersScopes.WEBMASTERS)) .build(); Webmasters service = new Webmasters.Builder(httpTransport, jsonFactory, credential) .setApplicationName(applicationName) .build(); SitesListResponse siteList = service.sites().list().execute(); if (siteList.isEmpty()) { logger.info("Site list is empty!"); } } catch (Exception e) { logger.error("Exception: ", e); } } } 

Is there something wrong with this code?

+6
source share
1 answer

After some debugging of the Google Oauth authentication code, my colleague discovered that there was nothing wrong with my code, but there was a certain procedure that should be followed in the Google Webmasters Console, which was not mentioned anywhere:

  • You need to grant "full" permission to the email address of your service account for each of your domains. If you previously gave "ownership", you must revoke this ownership (since you cannot change the permission if you give the right to give before giving permission).

  • You must provide ownership of your service account email address.

If you do it in the wrong order; your site will not be listed in the sitemaps list, and even if you explicitly try to get your site’s sitemaps at this address, you will receive an authentication error.

You must provide permissions and owners for each domain that you would like to access (even to get their names).

All your sites do not have a single top-level access control.

+6
source

All Articles