我正在嘗試從Ruby on Rails後端訪問Office 365 API,並遇到問題。如何從Ruby訪問Office 365 API?
無論我用的是ruby_outlook
寶石(github)或按照微軟的official Ruby on Rails sample,我得到401 unauthorized
。
我的access_token正在使用Omniauth保存,並且有效的,我檢查在here粘貼。
上午我使用的是正確的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
寶石):
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
寶石返回確切的錯誤是:
{"ruby_outlook_error"=>401}
當您將標記粘貼到JWT解析器中時,您對「scp」聲明有什麼看法? –
@JasonJohnston我看到「Contacts.Read Mail.Read Mail.ReadWrite Mail.Send User.Read」 – tommybond
好的。那麼'aud'聲明呢?你要求的範圍是什麼(在紅寶石漫遊中的'SCOPES'數組)? –