如何解决在 Python (V. 3.8.1) 中使用 OAuth2 身份验证时如何处理 HTTPError 403?
当尝试使用 Google 搜索控制台 API(特别是 OAuth 2.0)向 Google 搜索控制台查询某些指标(未显示)时,我收到 HTTPError 403:“返回”用户没有足够的权限访问站点“XXXXXXXX”。 com'。另见:https://support.google.com/webmasters/answer/2451999."。详细信息:“[{'message':”用户没有足够的权限访问站点 'https://www.xxxxxx.com'。另请参阅:https://support.google.com/webmasters/answer/2451999.",'domain': 'global','reason': 'forbidden'}]">"(用 x 替换了站点 URL)>
在其他一些尝试中,我也收到了 403 错误,提示我没有 Google Analytics(分析)帐户。
以下是授权访问的代码:
def get_domain_name(start_url):
domain_name = '{uri.netloc}'.format(uri=urlparse(start_url)) # Get Domain Name To Name Project
domain_name = domain_name.replace('.','_')
return domain_name
def create_project(directory):
if not os.path.exists(directory):
print('Create project: '+ directory)
os.makedirs(directory)
def authorize_creds(creds):
# Variable parameter that controls the set of resources that the access token permits.
ScopES = ['https://www.googleapis.com/auth/webmasters.readonly']
# Path to client_secrets.json file
CLIENT_SECRETS_PATH = creds
# Create a parser to be able to open browser for Authorization
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpformatter,parents=[tools.argparser])
flags = parser.parse_args([])
flow = client.flow_from_clientsecrets(
CLIENT_SECRETS_PATH,scope = ScopES,message = tools.message_if_missing(CLIENT_SECRETS_PATH))
# Prepare credentials and authorize HTTP
# If they exist,get them from the storage object
# credentials will get written back to a file.
storage = file.Storage('authorizedcreds.dat')
credentials = storage.get()
# If authenticated credentials don't exist,open browser to authenticate
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow,storage,flags)
http = credentials.authorize(http=httplib2.Http())
webmasters_service = build('webmasters','v3',http=http)
return webmasters_service
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。