如何在客户端c#app中使用Outlook REST API处理OAuth?

问题描述 投票:0回答:2

我正在尝试实现在客户端(编辑:安装Win10 UWP)应用程序中解释的here用于Outlook.com用户(没有O365,没有Azure)。正如在Mail API reference中所解释的那样,当前的客户端库还没有准备好与常规的Outlook.com用户和v2应用程序模型一起使用,因此我很想学习如何直接从客户端应用程序调用REST API。

具体来说,非常感谢有关如何处理登录和权限请求操作的c#代码示例。目前我知道如何使用System.Net.Http.HttpClient发送带有指定范围的GET请求,以及如何打开Web浏览器让用户登录,但是在他们授予他们的权限之后没有任何事情发生,因为浏览器不知道热处理重定向uri,似乎是每个安装的应用程序的标准。因此,我不知道如何在我的应用程序中使用授权代码接收响应消息。

有人可以解释如何处理这种情况给那些像我这样的新手吗?

编辑:正如我上面所说,我正在尝试安装已安装的应用程序,而不是Web应用程序。当然问题的基本逻辑是相同的,但我可以使用的库可能不是。具体来说,我正在开发Windows 10 UWP应用程序。

c# rest email outlook-restapi
2个回答
0
投票

dev.outlook.com上的tutorial适用于v2模型和Outlook.com。客户端库可以正常使用Outlook.com。这是ADAL的发布版本,不适用于v2 OAuth模型。 Azure团队推出了一个可以正常工作的“实验性”库,而Mail API的客户端库也可以使用它。


0
投票

这是不使用浏览器登录的示例。我们将使用outlook api和microsoft graph来实现。我们必须先登录并获取令牌。

public class AccessTokenModel
    {
        [JsonProperty("access_token")]
        public string AccessToken { get; set; }
    }

public static string GetToken()
            {
                using (HttpClient client = new HttpClient())
                {

                client.BaseAddress = new Uri("https://login.microsoftonline.com");

                var content = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair<string, string>("client_id", 'yourAppId'),
                new KeyValuePair<string, string>("client_secret", 'yourAppPassword'),
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("username", "emailAddress"),
                new KeyValuePair<string, string>("password", "emailPassword"),
                new KeyValuePair<string, string>("resource", "https://graph.microsoft.com"),
                new KeyValuePair<string, string>("scope", "openid")
                });

                var result = client.PostAsync($"/common/oauth2/token", content);
                var resultContent = result.Result.Content.ReadAsStringAsync();
                var model = JsonConvert.DeserializeObject<AccessTokenModel>(resultContent.Result);

                return model.AccessToken;

            }
        }

然后我们可以使用outlook api提供的服务。

© www.soinside.com 2019 - 2024. All rights reserved.