如何解决为什么收到“客户端错误:403客户端错误:...错误消息:访问被拒绝请检查凭据,然后重试”
from O365 import Account,MSGraPHProtocol
import datetime as dt
CLIENT_ID = 'f8384621-1095-4519-8999-0c7e616b7bc8'
SECRET_ID = '.s03m-t9u2q_9.Lpim5F.DTL-F1u4MBQy6'
credentials = (CLIENT_ID,SECRET_ID)
protocol = MSGraPHProtocol()
#protocol = MSGraPHProtocol(defualt_resource='<sharedcalendar@domain.com>')
scopes = ['User.Read']
account = Account(credentials,protocol=protocol)
if account.authenticate(scopes=scopes):
print('Authenticated!')
schedule = account.schedule()
calendar = schedule.get_default_calendar()
events = calendar.get_events(include_recurring=False)
# events = calendar.get_events(query=q,include_recurring=True)
for event in events:
print(event)
但不幸的是,在成功通过身份验证后,还会出现以下错误:
Client Error: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/me/calendar | Error Message: Access is denied. Check credentials and try again.
Traceback (most recent call last):
File "C:/Users/username/Documents/pycharmproba/main.py",line 18,in <module>
calendar = schedule.get_default_calendar()
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\calendar.py",line 1942,in get_default_calendar
response = self.con.get(url)
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\connection.py",line 805,in get
return self.oauth_request(url,'get',params=params,**kwargs)
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\connection.py",line 794,in oauth_request
return self._internal_request(self.session,url,method,line 756,in _internal_request
raise HTTPError('{} | Error Message: {}'.format(e.args[0],error_message),response=response) from None
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/me/calendar | Error Message: Access is denied. Check credentials and try again.
解决方法
您尚未添加访问日历事件的范围。将以下范围添加到您的代码中,然后重试。
protocol = MSGraphProtocol()
scopes = ['User.Read']
calendar_scopes = protocol.get_scopes_for('calendar_all')
scopes.extend(calendar_scopes)
account = Account(credentials,protocol=protocol)
if account.authenticate(scopes=scopes):
print('Authenticated!')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。