代码之家  ›  专栏  ›  技术社区  ›  Wayne

使用带有硬代码用户名和密码的Office Outlook API

  •  1
  • Wayne  · 技术社区  · 9 年前

    我希望web应用程序能够读取条目并将其添加到我的outlook日历中。 许多用户将使用web应用程序,但web应用程序只能访问一个outlook日历-我的。 我已经能够使用的所有示例都涉及到web应用程序用户的交互身份验证,但我的用户不会知道我的密码。 我想在web应用程序中硬编码我的用户名/电子邮件地址和密码。

    Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException:
    AADSTS65001: The user or administrator has not consented to use the application.
    Send an interactive authorization request for this user and resource.
    

    我不是房客的管理员。我有没有办法在没有管理员参与的情况下让它工作? 使用某种证书而不是用户名和密码作为用户凭据是否有帮助?

    我的代码(目前在一个简单的C#控制台应用程序中)如下:

    UserCredential uc = new UserCredential(MyUsername, MyPassword);
    var AuthContext = new AuthenticationContext("https://login.windows.net/Common");
    
    // this doesn't work unless an unexpired token already exists
    ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, uc);
    
    // this does work, but requires the app user to know the password
    ar = AuthContext.AcquireToken("https://outlook.office365.com/", MyClientId, new Uri(MyReturnURI));
    
    1 回复  |  直到 9 年前
        1
  •  2
  •   Fei Xue    8 年前

    我们可以使用 OAuth 2.0授权代码授予流 授予用户同意。下面是使用ADAL身份验证库的示例( )要获取委托令牌,请执行以下操作:

     static string authority= "https://login.microsoftonline.com/common";
     public static string GetDeligateToken(string resource, string clientId,string redirectURL)
        {
            AuthenticationContext authContext = new AuthenticationContext(authority);
            AuthenticationResult authResult= authContext.AcquireTokenAsync(resource, clientId,new Uri(redirectURL), new PlatformParameters(PromptBehavior.Auto)).Result;
            return authResult.AccessToken;
        }
    

    在我们同意应用程序后,现在我们可以使用您帖子中的代码来获取令牌。