简体   繁体   English

如何正确注册和访问OAuth2的Office 365 Graph API(使用Ruby中的omniauth)?

[英]How to properly register and access Office 365 Graph API for OAuth2 (using omniauth from Ruby)?

I'm trying to access the Office 365 (Graph API) from our Ruby on Rails application (specifically, the Calendar Read API). 我正在尝试从我们的Ruby on Rails应用程序(特别是Calendar Read API)访问Office 365(Graph API)。 We're using omniauth for our OAuth2 flows and as such, we have also tried to access the Graph API using the omniauth-office365 and the omniauth-microsoft-office365 gem. 我们在OAuth2流程中使用omniauth,因此,我们也尝试使用omn​​iauth-office365omn​​iauth-microsoft-office365 gem访问Graph API。 But I haven't been able to get an access token with neither of these gems so far. 但到目前为止,我还没有能够获得这些宝石的访问令牌。

I have registered our app in the Application Registration Portal , but any time I wanted to get Calendars.Read permission (using scope "profile https://graph.microsoft.com/calendar.read "), I always get the error 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我想获得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 . 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 Reading more articles about this, I got the impression that I need to actually go through Azure AD, so I signed up for that. 阅读更多关于此的文章,我得到的印象是我需要实际通过Azure AD,所以我注册了。 But then it seems I have to register a completely new web application in the Azure dashboard that has no link to the previously created application. 但是,似乎我必须在Azure仪表板中注册一个全新的Web应用程序,该应用程序没有链接到先前创建的应用程序。 I gave it a try, but that only results in a 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. 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. error. 错误。

I'm at the point where I'm completely confused. 我正处于完全困惑的地步。 What is the right way to go about this and to get this to work? 什么是正确的方法来实现这一目标? It cannot really be that I need to go through Azure AD, right? 我不一定需要通过Azure AD,对吗? What is the whole point of the Application Registration Portal then? 那么应用程序注册门户的重点是什么? It would be great if anyone could shed some light... 如果有人能说清楚的话会很棒......

Thanks, Pascal 谢谢,帕斯卡尔

The relationship between the Office 365 API and Azure AD is that Azure AD acts as an authorization server and the Office 365 API is a Resource Server registered with Azure AD. Office 365 API与Azure AD之间的关系是Azure AD充当授权服务器,而Office 365 API是在Azure AD中注册的资源服务器。

在此输入图像描述

Follow these steps to get your app working 请按照以下步骤操作您的应用

  1. Sign into the Microsoft App Registration Portal using either your personal or work or school account. 使用您的个人或工作或学校帐户登录Microsoft App Registration Portal。
  2. Choose Add an app. 选择添加应用。
  3. Enter a name for the app, and choose Create application. 输入应用程序的名称,然后选择“创建应用程序”。 The registration page displays, listing the properties of your app. 将显示注册页面,其中列出了您应用的属性。
  4. Copy the application ID. 复制应用程序ID。 This is the unique identifier for your app. 这是您应用的唯一标识符。
  5. Under Application Secrets, choose Generate New Password. 在“应用程序密钥”下,选择“生成新密码”。 Copy the app secret from the New password generated dialog. 从新密码生成对话框中复制应用程序密钥。
  6. You'll use the application ID and app secret to configure the app. 您将使用应用程序ID和应用程序密钥来配置应用程序。
  7. Under Platforms, choose Add platform > Web. 在“平台”下,选择“添加平台”>“Web”。 Make sure the Allow Implicit Flow check box is selected, and enter http://localhost:3000/auth/microsoft_v2_auth/callback as the Redirect URI. 确保选中Allow Implicit Flow复选框,并输入http:// localhost:3000 / auth / microsoft_v2_auth / callback作为Redirect URI。 The Allow Implicit Flow option enables the OpenID Connect hybrid flow. Allow Implicit Flow选项启用OpenID Connect混合流。 During authentication, this enables the app to receive both sign-in info (the id_token) and artifacts (in this case, an authorization code) that the app uses to obtain an access token. 在身份验证期间,这使应用程序能够接收应用程序用于获取访问令牌的登录信息(id_token)和工件(在本例中为授权代码)。 The redirect URI http://localhost:3000/auth/microsoft_v2_auth/callback is the value that the OmniAuth middleware is configured to use once it has processed the authentication request. 重定向URI http:// localhost:3000 / auth / microsoft_v2_auth / callback是OmniAuth中间件在处理完身份验证请求后配置使用的值。
  8. Choose Save. 选择保存。

you might find this SO thread interesting. 你可能会发现这个SO线程很有趣。 Also a working example of Accessing graph API in Rails here 在Rails的访问图形API的另外一个工作示例这里

Ok, after much fiddling around, I finally got a grip on things. 好吧,经过多次摆弄,我终于掌握了一切。 And it doesn't help that there are so many different ways of accessing the different API's, each carrying their specific version, and each with their whole slew of outdated "this is how you do it" articles. 没有帮助,还有访问不同的API,各持其特定版本的很多不同的方式,并各有其整体过时摆“这是你怎么做”的文章。

Let me summarize how I got everything to work and lessons learned. 让我总结一下我如何将一切工作和经验教训。

  1. There is an "old way" and a "new way" to gain access to the API's. 有一种“旧方式”和“新方法”可以访问API。 The old way (v1.0) involves Azure AD, where you have to get an Azure account and configure everything in that dashboard. 旧方法(v1.0)涉及Azure AD,您必须获得Azure帐户并配置该仪表板中的所有内容。 The new way (v2.0) involves the Application Registration Portal and just that. 新方法(v2.0)涉及应用程序注册门户网站 Also see this article . 另见本文
  2. The Graph API version is currently still at v1.0 and this version number has nothing to do with the authentication (Azure AD) version number described above. Graph API版本目前仍处于v1.0版本,此版本号与上述身份验证(Azure AD)版本号无关 (also see this article ) (另见本文
  3. I was using the omniauth-office365 and the omniauth-microsoft-office365 gems, both of which are trying to access the resources at the host https://outlook.office.com/ , whereas all examples refer to base https://graph.microsoft.com . 我使用的是omn​​iauth-office365omn​​iauth-microsoft-office365 gems,两者都试图访问主机https://outlook.office.com/的资源,而所有示例都引用了基础https://graph.microsoft.com Switching to the microsoft_v2_auth gem included in this Ruby sample got me further. 切换到这个Ruby示例中包含的microsoft_v2_auth gem让我更进一步。
  4. The AADSTS65005 seemed to have to do with the exact "wording" of the scopes. AADSTS65005似乎与范围的确切“措辞”有关。 I've seen wordings like :scope => 'openid email profile offline_access https://graph.microsoft.com/calendar.read' , but the correct wording is :scope => 'openid email profile offline_access https://graph.microsoft.com/Calendars.Read' (so plural Calendars and Pascal case). 我见过这样的措辞: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案例)。 This seemed to solve the problem for me. 这似乎解决了我的问题。
  5. The "Insufficient privileges to complete the operation." "Insufficient privileges to complete the operation." occured after a succesful callback with an access token, but right when the gem wanted to get extra profile information from the /v1.0/me API. 在使用访问令牌成功回调之后发生,但是当gem想要从/v1.0/me API获取额外的配置文件信息时。 Only after I added https://graph.microsoft.com/User.Read to the scope in my Ruby application, as well as User.Read grant in the application registration, the gem seemed to have the permissions it needed and the error went away. 只有在我将https://graph.microsoft.com/User.Read添加到我的Ruby应用程序中的范围以及应用程序注册中的User.Read授权之后,gem似乎才具有所需的权限并且错误已经发生远。 NOTE! 注意! It seems updating your application configuration can take up to 30 minutes to take effect! 似乎更新您的应用程序配置最多可能需要30分钟才能生效! This makes it so damn hard to make any progression and find exactly what actions have what effect. 这使得进行任何进展都非常困难,并确切地发现了什么行动会产生什么影响。
  6. Microsoft access tokens expire within one hour, so you will need to refresh your access token often, using a refresh token. Microsoft访问令牌将在一小时内过期,因此您需要使用刷新令牌经常刷新访问令牌。 You get a refresh token with your initial authorization request, only if you include offline_access in your scope (see point 4). 只有在范围中包含offline_access ,才会获得具有初始授权请求的刷新令牌(请参阅第4点)。 Then you can use the following type of code: 然后您可以使用以下类型的代码:

     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 

Also, when testing this it is invaluable to revoke the access token you've acquired during earlier tests. 此外,在测试时,撤销您在早期测试中获得的访问令牌是非常宝贵的。 This can be done at myapps.microsoft.com . 这可以在myapps.microsoft.com上完成。

I've also run into CSRF errors in this process, in which case you need to clear your cache. 我也在这个过程中遇到CSRF错误,在这种情况下你需要清除你的缓存。

If I find anything else of interest, I'll add it here, in the hopes that noone will have to wander long in these murky API forests. 如果我发现其他任何有趣的东西,我会在这里添加它,希望没有人会在这些阴暗的API森林中长时间徘徊。 :( :(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Ruby中访问Office 365 API? - How to hit Office 365 API from Ruby? 如何在不使用omniauth的情况下使用oauth2连接到网站? - How to connect to website using oauth2 WITHOUT using omniauth? 访问Office 365中的其他用户日历时从Graph API访问被拒绝或找不到错误 - Access denied or Not Found errors from Graph API while Access other users calendar in Office 365 Omniauth谷歌oauth2战略与离线访问 - Omniauth google oauth2 strategy with offline access 结合使用Google OAuth2 API和Ruby on Rails - Using Google OAuth2 API with Ruby on Rails Google Oauth2 API-Omniauth Rails - Google Oauth2 api - Omniauth Rails 如何解救OmniAuth :: Strategies :: OAuth2 :: CallbackError? - How to rescue OmniAuth::Strategies::OAuth2::CallbackError? 无法使用 Ruby on Rails、Devise、Omniauth 在 Google 和玩具网站之间设置简单的 OAuth2 - Can't setup simple OAuth2 between Google and toy website using Ruby on Rails, Devise, Omniauth 来自 Outlook rest API 的 omniauth-office365 gem 权限错误 - omniauth-office365 gem permission error from Outlook rest API 从Office 365返回的OAuth2令牌不包含preffered_username声明 - OAuth2 token returned from Office 365 doesn't contain a preffered_username claim
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM