如何从Ruby中访问Office 365 API? [英] How to hit Office 365 API from Ruby?
问题描述
我正在尝试从Ruby on Rails后端访问Office 365 API,并且遇到问题.
I am attempting to access the Office 365 API from a Ruby on Rails backend and am having problems.
我是否使用ruby_outlook
gem( github)或关注Microsoft的
Whether I use the ruby_outlook
gem (github) or follow Microsoft's official Ruby on Rails sample, I am getting 401 unauthorized
.
我的access_token正在使用Omniauth保存,并且有效,我通过将其粘贴到此处中进行了检查.
My access_token is being saved using Omniauth and is valid, I checked by pasting it in here.
我使用正确的access_token吗?它的长度超过1400个字符(准确地说是1442个字符).谁能给我看一个如何从Ruby正确调用Office 365邮件API的示例吗?
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
代码示例(使用ruby_outlook
gem):
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
ruby_outlook
gem返回的确切错误是:
The exact error that the ruby_outlook
gem returns is:
{"ruby_outlook_error"=>401}
推荐答案
问题是令牌中的作用域与所使用的API端点之间不匹配.范围必须与端点匹配.
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.
在您的情况下,您请求了图形API 范围,但您正在调用 Outlook API 端点.
In your case, you requested a Graph API scope, but you're calling the Outlook API endpoint.
您只需要在一个位置注册客户ID和密码: https://apps.dev. microsoft.com .听起来您可能还已经在Azure管理门户中注册了一个应用程序(要求您在注册本身中指定作用域).
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).
确保您使用的是来自apps.dev.microsoft.com的客户端ID,并确保您的范围要求为" https://outlook.office.com 的范围,您应该会很高兴.
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.
如果Omniauth策略依赖于Azure的v1身份验证终结点,则可能需要您在Azure管理门户中进行注册.在这种情况下,请忘记我对apps.dev.microsoft.com所说的内容,而是更改您的应用程序注册以使用Microsoft Exchange Online的适当权限.
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.
更新:根据您的评论,Omniauth策略确实需要v1 Azure身份验证/令牌终结点,因此,如果要继续使用该策略,则有2个选择:
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:
- 更改代码以使用Graph端点.您需要使用上面的Faraday选项(ruby_outlook是为Outlook端点设计的),并将URL更改为
https://graph.microsoft.com
,将request.url
更改为/v1.0/me/contacts
. - 在 https://dev.outlook.com/appregistration 上创建新的应用注册,为您的代码创建适当的范围.您需要一个Office 365帐户才能登录到应用程序注册工具.
- 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 therequest.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.
这篇关于如何从Ruby中访问Office 365 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!