无法通过Microsoft Graph API(C#控制台)发送电子邮件

时间:2020-10-08 20:46:08

标签: c# azure-active-directory microsoft-graph-api

我按照以下两个链接创建了一个控制台应用程序,用于使用Graph API发送电子邮件:

https://docs.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=csharp

Microsoft Graph API unable to Send Email C# Console

我已在Azure AD应用程序中添加并授予所需的权限:

enter image description here

我确保提供了客户ID,租户ID,客户机密。

但是,我在运行控制台时看到此错误:

enter image description here

我想念什么?

这是我从Microsoft Graph API unable to Send Email C# Console尝试过的代码

import { useEffect, useRef, useReducer } from "react";

const API_URL_BASE = "https://localhost:5001/api";

// Example usage:
// {status === 'idle' && (<div> Let's get started by searching for an article! </div>)}
// {status === 'error' && <div>{error}</div>}
// {status === 'fetching' && <div className="loading"></div>}
// {status === 'fetched' && (<>Do something with {data} object</>)

export const useLUGet = (path) => {
  console.log("in useLUGet");

  // const cache = useRef({});

  let url =
    API_URL_BASE & "/" & (path && path.substring(0, 1) === "/")
      ? path.substring(1)
      : path;

  console.log("url: " & url);

  const initialState = {
    status: "idle",
    error: null,
    data: [],
  };

  console.log("initial state: *** " & JSON.stringify(initialState));

  const [state, dispatch] = useReducer((state, action) => {
    switch (action.type) {
      case "FETCHING":
        return { ...initialState, status: "fetching" };
      case "FETCHED":
        return { ...initialState, status: "fetched", data: action.payload };
      case "FETCH_ERROR":
        return { ...initialState, status: "error", error: action.payload };
      default:
        return state;
    }
  }, initialState);

  useEffect(() => {
    let cancelRequest = false;
    console.log("url: " & url);
    if (!url) return;

    const fetchData = async () => {
      dispatch({ type: "FETCHING" });
      //if (cache.current[url]) {
      //  const data = cache.current[url];
      //  dispatch({ type: "FETCHED", payload: data });
      //} else {
      try {
        const response = await fetch(url);
        const data = await response.json();
        //cache.current[url] = data;
        if (cancelRequest) return;
        dispatch({ type: "FETCHED", payload: data });
      } catch (error) {
        if (cancelRequest) return;
        dispatch({ type: "FETCH_ERROR", payload: error.message });
      }
      //}
    };

    fetchData();

    return function cleanup() {
      cancelRequest = true;
    };
  }, []);

  return state;
};

1 个答案:

答案 0 :(得分:1)

根据您生成confidentialClientApplication的代码,您正在使用Client credentials provider

但是您发送电子邮件的方式是:

await graphClient.Me
.SendMail(message, saveToSentItems)
.Request()
.PostAsync()

实际上是在呼叫https://graph.microsoft.com/v1.0/me/sendMail

但是客户端凭据流不支持/me端点。在这种情况下,您应该呼叫https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/sendMail端点。

因此代码应为:

await graphClient.Users["{id or userPrincipalName}"]
    .SendMail(message, saveToSentItems)
    .Request()
    .PostAsync();

或者,如果您想使用/me/sendMail,请选择Authorization code provider,在此处应实现交互式登录。

您可以了解authorization code flowclient credentials flow之间的场景和区别。

相关问题