【发布时间】:2019-09-10 10:38:33
【问题描述】:
我创建了一个 Ruby 应用程序来使用 Microsoft Graph API 发送电子邮件。 我在没有用户的情况下获得访问令牌,基于: https://docs.microsoft.com/en-us/graph/auth-v2-service#5-use-the-access-token-to-call-microsoft-graph
url = URI.parse("https://login.microsoftonline.com/common/oauth2/v2.0/token")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
req = Net::HTTP::Post.new(url.request_uri)
req.set_form_data({
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SCERET,
'grant_type' => 'client_credentials',
'scope' => 'https://graph.microsoft.com/.default'
})
response = http.request(req)
我得到了令牌。
接下来,我想通过我的应用程序在门户 azure 上向任何用户发送邮件,该应用程序具有 api 权限。
然后,我调用 send_mail 函数。
url = URI.parse("https://graph.microsoft.com/v1.0/users/{MYID}/sendMail")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == "https")
req = Net::HTTP::Post.new(url.request_uri)
req["Authorization"] = "Bearer #{get_token_without_user}"
req["Content-type"] = "application/json"
req.set_form_data({
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
}
},
"toRecipients": [
{
"emailAddress": {
"address": "to_email@gmail.com"
}
}],
"saveToSentItems": "false"
})
response = http.request(req)
我收到此错误:
那么,谁能指导我我的错误在哪里?非常感谢!
【问题讨论】:
-
我认为您必须为此检查 Microsoft 的 API。
-
@VatsalJain 我更新了更多来源以获取令牌。没事吧?
-
url 中缺少 v2.0 并尝试添加 Content-Type 的标头
-
对不起,它仍然无法正常工作。 :(
-
这将有助于查看对图形端点进行实际调用的源代码。也许请求有问题,而不是令牌......