【问题标题】:How to handle OAuth with Outlook REST API in client c# app?如何在客户端 c# 应用程序中使用 Outlook REST API 处理 OAuth?
【发布时间】:2015-09-09 10:58:07
【问题描述】:

我正在尝试在适用于 Outlook.com 用户(没有 O365,没有 Azure)的客户端(编辑:安装的 Win10 UWP)应用程序中实现 here 的解释。正如Mail API reference 中所述,当前的客户端库还不能用于普通 Outlook.com 用户和 v2 应用模型,因此我很想学习如何直接从客户端应用调用 REST API。

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

有人可以向像我这样不熟悉这种事情的人解释如何处理这种情况吗?

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

【问题讨论】:

    标签: c# rest email outlook-restapi


    【解决方案1】:

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

    【讨论】:

    • 对不起,我应该更清楚一点,我正在尝试在已安装的应用程序中实现该过程,而不是网络应用程序。我正在做的事情的逻辑可能是相同的,但例如我不能使用System.Web.Mvc。具体来说,我正在尝试使用 Windows 10 UWP 应用程序。
    • 您找到解决方案了吗?
    【解决方案2】:

    这是不使用浏览器登录的示例。我们将使用 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提供的服务了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2021-01-27
      • 2021-06-07
      • 2014-01-30
      • 2020-05-28
      • 2012-09-24
      • 1970-01-01
      相关资源
      最近更新 更多