如何使用python访问谷歌凭据的网站页面?



我在获得凭证后尝试访问谷歌生态系统页面。我的应用程序将管理日历事件,使用python和谷歌api。我有证书,列出事件,阅读摘要等。但是,如果我尝试访问一个网页(与AuthorizedSession类):

https://calendar.google.com

我注意到这个页面要求谷歌提供登录/密码。可能这个任务超出了指定的范围。想法吗?

提前感谢!

代码:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.auth.transport.requests import AuthorizedSession
import pickle
import os.path
import requests
from pathlib import Path

SCOPES = ['https://www.googleapis.com/auth/calendar']
CREDENTIALS_FILE = 'client_secret_123456ABCDEFG.apps.googleusercontent.com.json'
def get_credentials():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIALS_FILE, SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return creds

authed_session = AuthorizedSession(get_credentials())
response = authed_session.request(
'GET', 'https://calendar.google.com/')
print(response.status_code)
....

url calendar.google.com与API不在同一个作用域。

这是API文档的一部分:

下面是Google日历API的OAuth 2.0作用域信息:作用域含义https://www.googleapis.com/auth/calendar读写访问日历https://www.googleapis.com/auth/calendar.readonly对日历的只读访问https://www.googleapis.com/auth/calendar.events读/写访问事件https://www.googleapis.com/auth/calendar.events.readonly对事件的只读访问https://www.googleapis.com/auth/calendar.settings.readonly只读访问设置https://www.googleapis.com/auth/calendar.addons.execute作为一个日历插件

编辑:Google日历API的所有操作都可以在文档 中描述的端点中使用。

相关内容

最新更新