OneDrive for Business Oauth2身份验证

时间:2014-05-14 11:30:14

标签: c# ms-office onedrive

我正在尝试使用OAuth2.0身份验证协议访问OneDrive for Business。我跟着这个例子: http://msdn.microsoft.com/EN-US/library/dn605894(v=office.15).aspx 这是我目前的代码:

        //  Create an authentication context
        AuthenticationContext ac = new AuthenticationContext(string.Format("https://login.windows.net/{0}",
        ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value));

        String id = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;

        //  Create a client credential based on the application id and secret.
        ClientCredential clcred = new ClientCredential(AppPrincipalId, AppKey);

        //  Using the authorization code acquire an access token.
        var arAD = ac.AcquireTokenByAuthorizationCode(code, new Uri(appRedirect), clcred);

我收到的授权代码格式错误。我不明白为什么我收到这条消息。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

当我尝试手动获取授权代码然后在控制台应用程序中使用它时,会出现同样的问题。不知道为什么它对我不起作用。

但是当我从Web App使用它时,同样的应用程序执行授权并收到代码作为回报(因为我将其指定为returnURL) - 一切正常。

我建议你看看这个例子

https://github.com/AzureADSamples/WebApp-WebAPI-OAuth2-UserIdentity-DotNet

还有这篇文章要了解在好的情况下发生了什么:

http://blogs.msdn.com/b/exchangedev/archive/2014/03/25/using-oauth2-to-access-calendar-contact-and-mail-api-in-exchange-online-in-office-365.aspx

P.S。 它不是OneDrive,但我有完全相同的问题,所以如果你可以使用Exchange或GraphApi,那么它也适用于OneDrive。

答案 1 :(得分:0)

如果您以不正确的格式发送身份验证代码,则授权代码出现格式错误。可能是您发送的授权码是authcode和会话状态的编码值。您需要分离这两个值并仅发送身份验证代码。或解码'&' auth代码和会话状态之间的符号(分隔符),并将它们作为两个参数发送。

来自ADAL4j的方法 AcquireTokenByAuthorizationCode(...)仅采用authcode。

相关问题