[英]How to properly register and access Office 365 Graph API for OAuth2 (using omniauth from Ruby)?
我正在尝试从我们的Ruby on Rails应用程序(特别是Calendar Read API)访问Office 365(Graph API)。 我们在OAuth2流程中使用omniauth,因此,我们也尝试使用omniauth-office365和omniauth-microsoft-office365 gem访问Graph API。 但到目前为止,我还没有能够获得这些宝石的访问令牌。
我已经在应用程序注册门户中注册了我们的应用程序 ,但是AADSTS65005: The client application has requested access to resource 'https://graph.windows.net/'. This request has failed because the client has not specified this resource in its requiredResourceAccess list
我想获得Calendars.Read权限(使用范围“profile https://graph.microsoft.com/calendar.read ”),我总是会收到错误AADSTS65005: The client application has requested access to resource 'https://graph.windows.net/'. This request has failed because the client has not specified this resource in its requiredResourceAccess list
AADSTS65005: The client application has requested access to resource 'https://graph.windows.net/'. This request has failed because the client has not specified this resource in its requiredResourceAccess list
。 阅读更多关于此的文章,我得到的印象是我需要实际通过Azure AD,所以我注册了。 但是,似乎我必须在Azure仪表板中注册一个全新的Web应用程序,该应用程序没有链接到先前创建的应用程序。 我试了一下,但这只会导致AADSTS70002: Error validating credentials. AADSTS50011: The reply address 'https://example.com/auth/office365/callback?code=AQABA...a_very_long_string&session_state=e1029a3b-f6a5-4e7a-940e-18a21ee4c44f' does not match the reply address 'https://example.com/auth/office365/callback' provided when requesting Authorization code.
AADSTS70002: Error validating credentials. AADSTS50011: The reply address 'https://example.com/auth/office365/callback?code=AQABA...a_very_long_string&session_state=e1029a3b-f6a5-4e7a-940e-18a21ee4c44f' does not match the reply address 'https://example.com/auth/office365/callback' provided when requesting Authorization code.
错误。
我正处于完全困惑的地步。 什么是正确的方法来实现这一目标? 我不一定需要通过Azure AD,对吗? 那么应用程序注册门户的重点是什么? 如果有人能说清楚的话会很棒......
谢谢,帕斯卡尔
Office 365 API与Azure AD之间的关系是Azure AD充当授权服务器,而Office 365 API是在Azure AD中注册的资源服务器。
请按照以下步骤操作您的应用
好吧,经过多次摆弄,我终于掌握了一切。 它没有帮助,还有访问不同的API,各持其特定版本的很多不同的方式,并各有其整体过时摆“这是你怎么做”的文章。
让我总结一下我如何将一切工作和经验教训。
https://outlook.office.com/
的资源,而所有示例都引用了基础https://graph.microsoft.com
。 切换到这个Ruby示例中包含的microsoft_v2_auth gem让我更进一步。 AADSTS65005
似乎与范围的确切“措辞”有关。 我见过这样的措辞:scope => 'openid email profile offline_access https://graph.microsoft.com/calendar.read'
,但正确的措辞是:scope => 'openid email profile offline_access https://graph.microsoft.com/Calendars.Read'
(所以复数日历和Pascal案例)。 这似乎解决了我的问题。 "Insufficient privileges to complete the operation."
在使用访问令牌成功回调之后发生,但是当gem想要从/v1.0/me
API获取额外的配置文件信息时。 只有在我将https://graph.microsoft.com/User.Read
添加到我的Ruby应用程序中的范围以及应用程序注册中的User.Read
授权之后,gem似乎才具有所需的权限并且错误已经发生远。 注意! 似乎更新您的应用程序配置最多可能需要30分钟才能生效! 这使得进行任何进展都非常困难,并确切地发现了什么行动会产生什么影响。 Microsoft访问令牌将在一小时内过期,因此您需要使用刷新令牌经常刷新访问令牌。 只有在范围中包含offline_access
,才会获得具有初始授权请求的刷新令牌(请参阅第4点)。 然后您可以使用以下类型的代码:
oauth = OmniAuth::Strategies::MicrosoftV2Auth.new( nil, ENV['OFFICE365_KEY'], ENV['OFFICE365_SECRET'] ) token = OAuth2::AccessToken.new( oauth.client, @access_token, { refresh_token: @refresh_token } ) new_token = token.refresh! @access_token = new_token.token if new_token.token
此外,在测试时,撤销您在早期测试中获得的访问令牌是非常宝贵的。 这可以在myapps.microsoft.com上完成。
我也在这个过程中遇到CSRF错误,在这种情况下你需要清除你的缓存。
如果我发现其他任何有趣的东西,我会在这里添加它,希望没有人会在这些阴暗的API森林中长时间徘徊。 :(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.