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?
source share