如何从Ruby访问Office 365 API?(How to hit Office 365 API from Ruby?)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
如何从Ruby访问Office 365 API?(How to hit Office 365 API from Ruby?)

我试图从Ruby on Rails后端访问Office 365 API并遇到问题。

无论我使用ruby_outlook gem( github)还是遵循微软官方的Ruby on Rails示例 ,我都会获得401 unauthorized 。

我的access_token正在使用Omniauth保存并且有效,我通过在此处粘贴来检查。

我使用正确的access_token吗? 它超过1400个字符(确切地说是1442个字符)。 有人能告诉我如何从Ruby正确调用Office 365 Mail API的示例吗?

代码示例(使用法拉第):

key = @auth[:key] conn = Faraday.new(:url => 'https://outlook.office.com') do |faraday| # Outputs to the console faraday.response :logger # Uses the default Net::HTTP adapter faraday.adapter Faraday.default_adapter end response = conn.get do |request| request.url '/api/v2.0/me/contacts' request.headers['Authorization'] = "Bearer #{key}" request.headers['Accept'] = 'application/json' end

代码示例(使用ruby_outlook gem):

client = RubyOutlook::Client.new key = @auth[:key] page = 1 view_size = 30 fields = [ 'DisplayName', 'EmailAddresses' ] sort = {:sort_field => 'DisplayName', :sort_order => 'ASC'} contacts = client.get_contacts key, view_size, page, fields, sort

ruby_outlook gem返回的确切错误是:

{"ruby_outlook_error"=>401}

I am attempting to access the Office 365 API from a Ruby on Rails backend and am having problems.

Whether I use the ruby_outlook gem (github) or follow Microsoft's official Ruby on Rails sample, I am getting 401 unauthorized.

My access_token is being saved using Omniauth and is valid, I checked by pasting it in here.

Am I using the correct access_token? It is over 1400 characters long (1442 to be exact). Can anyone show me an example of how to properly call the Office 365 Mail API from Ruby?

Code Example (using Faraday):

key = @auth[:key] conn = Faraday.new(:url => 'https://outlook.office.com') do |faraday| # Outputs to the console faraday.response :logger # Uses the default Net::HTTP adapter faraday.adapter Faraday.default_adapter end response = conn.get do |request| request.url '/api/v2.0/me/contacts' request.headers['Authorization'] = "Bearer #{key}" request.headers['Accept'] = 'application/json' end

Code Example (using ruby_outlook gem):

client = RubyOutlook::Client.new key = @auth[:key] page = 1 view_size = 30 fields = [ 'DisplayName', 'EmailAddresses' ] sort = {:sort_field => 'DisplayName', :sort_order => 'ASC'} contacts = client.get_contacts key, view_size, page, fields, sort

The exact error that the ruby_outlook gem returns is:

{"ruby_outlook_error"=>401}

最满意答案

问题是令牌中的范围与您正在使用的API端点之间不匹配。 范围必须与端点匹配。

在您的情况下,您请求了一个Graph API范围,但您正在调用Outlook API端点。

您只需在一个地方注册您的客户ID和密码: https : //apps.dev.microsoft.com 。 听起来您可能还在Azure管理门户中注册了一个应用程序(这要求您在注册本身中指定范围)。

确保您使用的是apps.dev.microsoft.com中的客户端ID,并确保您的范围是“ https://outlook.office.com ”范围,您应该很高兴。

Omniauth策略可能要求您在Azure管理门户中注册,如果它们依赖于Azure的v1 auth端点。 在这种情况下,请忘记我对apps.dev.microsoft.com所说的内容,而是更改您的应用注册以使用Microsoft Exchange Online的相应权限。

更新 :根据您的意见,Omniauth策略需要v1 Azure身份验证/令牌端点,因此如果您想继续使用该策略,则有2个选项:

更改代码以使用Graph端点。 您需要使用上面的法拉第选项(ruby_outlook是为Outlook端点设计的),并将您的URL更改为https://graph.microsoft.com ,将request.url更改为/v1.0/me/contacts 。 在https://dev.outlook.com/appregistration创建一个新的应用注册,这将为您的代码创建适当的范围。 您需要Office 365帐户才能登录应用程序注册工具。

The problem is a mismatch between the scopes in your token and the API endpoint you're using. The scope has to match the endpoint.

In your case, you requested a Graph API scope, but you're calling the Outlook API endpoint.

You should only have to register in one place for your client ID and secret: https://apps.dev.microsoft.com. It sounds like you may have also registered an app in the Azure Management Portal (which requires you to specify scopes in the registration itself).

Make sure you're using a client ID from apps.dev.microsoft.com and make sure your scopes are requested as 'https://outlook.office.com' scopes, and you should be good to go.

That Omniauth strategy might require that you register in the Azure Management Portal if they are dependent on Azure's v1 auth endpoints. In that case, forget what I said about apps.dev.microsoft.com and instead change your app registration to use the appropriate permissions from Microsoft Exchange Online.

UPDATE: Based on your comments, that Omniauth strategy DOES require the v1 Azure auth/token endpoints, so you have 2 options if you want to keep using that strategy:

Change your code to use the Graph endpoints. You'll need to use the Faraday option above (ruby_outlook is designed for the Outlook endpoints), and change your URL to https://graph.microsoft.com, and the request.url to /v1.0/me/contacts. Create a new app registration at https://dev.outlook.com/appregistration, which will create the proper scopes for your code. You'll need an Office 365 account to login to the app registration tool.

更多推荐

本文发布于:2023-04-15 03:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/395dc75f144cba15b9a75a6535f3fe58.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Office   Ruby   hit   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!