i'm using a service account to successfully access the webmaster tools api via python.
my code is below.
problem is that all i receive is an empty set "{}" for the line "print site_list"
when i use the google api explorer in a browser, i get two sites.
any ideas? please note i get an error on the following line of code, b/c we cannot cycle through an empty set.
import json
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build
with open("privatekey.pem") as f:
private_key = f.read()
credentials = SignedJwtAssertionCredentials(client_email, private_key,
http = Http()
credentials.authorize(http)
webmasters_service = build('webmasters', 'v3', http=http)
# Retrieve list of websites in account
site_list = webmasters_service.sites().list().execute()
print site_list
# Remove all unverified sites
verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry'] if s['permissionLevel'] != 'siteUnverifiedUser']
# This is just ALL sites, not just verified sites
sites_urls = [s['siteUrl'] for s in site_list['siteEntry']]
print verified_sites_urls
# Printing the urls of all sites you are verified for.
for site_url in verified_sites_urls:
print site_url
# Retrieve list of sitemaps submitted
sitemaps = webmasters_service.sitemaps().list(siteUrl=site_url).execute()
if 'sitemap' in sitemaps:
sitemap_urls = [s['path'] for s in sitemaps['sitemap']]
print " " + "\n ".join(sitemap_urls)