using a service account returns empty set

35 views
Skip to first unread message

Mike (gotmike.com)

unread,
Apr 24, 2015, 5:23:27 PM4/24/15
to google-api-p...@googlegroups.com
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)

mikael...@leadingfellows.com

unread,
May 18, 2015, 11:42:45 AM5/18/15
to google-api-p...@googlegroups.com
Hello,
i've got exactly the same behavior  (got almost the same sample code),
i've got some validated websites in my webmasters tools dashboard,
and the "sites()" fonction from the service object returns an empty result. 

thanks in advance,
Reply all
Reply to author
Forward
0 new messages