我正在编写一个 Python 脚本以将站点地图连续提交到 Search Console。Python 快速入门示例工作正常:https ://developers.google.com/webmaster-tools/search-console-api-original/v3/quickstart/quickstart-python
我尝试使用以下代码提交我的站点地图:
CLIENT_ID = 'xxxxx'
CLIENT_SECRET = 'yyyyyy'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters'
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print ('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
webmasters_service = build('webmasters', 'v3', http=http)
WEBSITE = 'http://www.mywebsite.com'
SITEMAP_PATH = 'http://www.mywebsite.com/sitemaps/'
SITEMAPS_LIST = ['sitemap1.xml','sitemap2.xml','sitemap3.xml']
print ('Adding sitemaps to website ' + WEBSITE)
for sitemap in SITEMAPS_LIST:
print (' '+SITEMAP_PATH + sitemap)
webmasters_service.sitemaps().submit(siteUrl=WEBSITE, feedpath=SITEMAP_PATH + sitemap)
没有错误。但是站点地图没有添加到我的 Search Console 中。如果我尝试在 Search Console 中或通过 API 资源管理器 Web 界面手动添加文件,它就可以工作。