Search Console Api 403用户没有足够的权限

时间:2015-09-17 12:05:59

标签: python api google-webmaster-tools google-api-python-client

我正在使用以下代码来获取特定域中的klicks,展示次数,ctr和位置。

import argparse
import sys
from googleapiclient import sample_tools
import json

argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('property_uri', type=str,
                       help=('Site or app URI to query data for (including '
                             'trailing slash).'))
argparser.add_argument('start_date', type=str,
                       help=('Start date of the requested date range in '
                             'YYYY-MM-DD format.'))
argparser.add_argument('end_date', type=str,
                       help=('End date of the requested date range in '
                             'YYYY-MM-DD format.'))

def main(argv):
  service, flags = sample_tools.init(
      argv, 'webmasters', 'v3', __doc__, __file__, parents=[argparser],
      scope='https://www.googleapis.com/auth/webmasters.readonly')

  request = {
      'startDate': flags.start_date,
      'endDate': flags.end_date,
      'dimensions': ['query'],
      'rowLimit': 10
  }
  response = execute_request(service, flags.property_uri, request)
  print json.dumps(response, sort_keys=True, indent=4)
  print_table(response, 'Top Queries')

def execute_request(service, property_uri, request):
  """Executes a searchAnalytics.query request.

  Args:
    service: The webmasters service to use when executing the query.
    property_uri: The site or app URI to request data for.
    request: The request to be executed.

  Returns:
    An array of response rows.
  """
  return service.searchanalytics().query(
      siteUrl=property_uri, body=request).execute()


def print_table(response, title):
  """Prints out a response table.

  Each row contains key(s), clicks, impressions, CTR, and average position.

  Args:
    response: The server response to be printed as a table.
    title: The title of the table.
  """
  print title + ':'

  if 'rows' not in response:
    print 'Empty response'
    return

  rows = response['rows']
  row_format = '{:<20}' + '{:>20}' * 4
  print row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position')
  for row in rows:
    keys = ''
    # Keys are returned only if one or more dimensions are requested.
    if 'keys' in row:
      keys = u','.join(row['keys']).encode('utf-8')
    print row_format.format(
        keys, row['clicks'], row['impressions'], row['ctr'], row['position'])

if __name__ == '__main__':
  main(sys.argv)

当我在搜索控制台的Web界面中列出的域(例如www.example.de)中输入时,该脚本可以正常工作。 但是,当我试图获取另一个网站的值,这没有在网络界面(可能是www.example.de/sub_site)中列出,我收到一条错误消息,我没有这个权限。 (在Webinterface上我有完全权限,可以看到值) 问题是,我只需要来自子网站的值。 所以我的问题是,api中没有启用此功能,或者我的脚本中是否有错误?

1 个答案:

答案 0 :(得分:0)

您必须使用dimensionFilterGroups并按page尺寸 - example filter by country进行过滤。另请参阅official documentation

相关问题