我正在玩 OneDrive SDK 1.1.15.0:
try
{
AppConfig appConfig = new AppConfig
{
MicrosoftAccountAppId = oneDriveClientID, //something like 00000000123456AB
MicrosoftAccountClientSecret = oneDriveClientSecret, //something like 3vx[...]1sJ
MicrosoftAccountReturnUrl = "https://localhost/return",
MicrosoftAccountScopes = new string[] { "wl.signin", "wl.offline_access", "onedrive.readonly" }
};
OneDriveClient oneDriveClient = new OneDriveClient(appConfig);
AccountSession accountSession = await oneDriveClient.AuthenticateAsync();
//more code
await oneDriveClient.SignOutAsync();
}
catch (Exception ex)
{
throw ex;
}
我的问题是:
AccountSession accountSession = await oneDriveClient.AuthenticateAsync();
抛出以下异常:
Microsoft.OneDrive.Sdk.OneDriveException, AuthenticationFailure: Failed to retrieve a valid authentication token for the user.
有什么想法吗?
提前致谢!
更新
阅读 ginach 的评论(谢谢!)后,我更新了我的代码。一些需要强调的论点:
- 我想从 Azure worker 角色访问 OneDrive,因此没有身份验证窗口或类似的东西。
- 我将 Microsoft.OneDrive SDK 上传到 1.1.20 版本。
- 我已经将我的应用程序注册到 OneDrive 开发门户。
我的实际代码是:
try
{
MicrosoftAccountServiceInfo serviceInfo = new MicrosoftAccountServiceInfo();
serviceInfo.AppId = oneDriveClientID; //something like: 00000000ABCDEFGH
serviceInfo.ClientSecret = oneDriveClientSecret; //something like: 3vx[...]1sJ
serviceInfo.ReturnUrl = oneDriveReturnUrl; //something like: https://localhost/return
serviceInfo.Scopes = oneDriveAccountScopes; //something like new string[] { "wl.signin", "wl.offline_access", "onedrive.readonly" }
MicrosoftAccountAuthenticationProvider authenticationProvider = new MicrosoftAccountAuthenticationProvider(serviceInfo);
OneDriveClient oneDriveClient = await OneDriveClient.GetAuthenticatedMicrosoftAccountClient(oneDriveClientID, oneDriveReturnUrl, oneDriveAccountScopes, authenticationProvider);
//more code
await oneDriveClient.SignOutAsync();
}
catch (OneDriveException odex)
{
throw odex;
}
catch (Exception ex)
{
throw ex;
}
我一次又一次地获得(在 OneDriveClient.GetAuthenticatedMicrosoftAccountClient 方法中)一个 OneDriveException 声明(错误属性):AuthenticationFailure - 无法为用户检索有效的身份验证 token 。
有什么建议吗?
谢谢。
更新 2
好的,我正在尝试一种新方法。使用 RestSharp,我尝试使用该代码登录 OneDrive:
string clientId = "00[...]00";
string scopes = "wl.signin, wl.offline_access, onedrive.readonly";
string responseType = "code";
string redirectUri = "https://login.live.com/oauth20_desktop.srf";
RestClient client = new RestClient("https://login.live.com");
RestRequest request = new RestRequest();
request.Method = Method.GET;
request.Resource = "oauth20_authorize.srf";
request.AddQueryParameter("client_id", clientId);
request.AddQueryParameter("scope", scopes);
request.AddQueryParameter("response_type", responseType);
request.AddQueryParameter("redirect_uri", redirectUri);
IRestResponse response = client.Execute(request);
string content = response.Content;
我用 Fiddler 检查请求,我发送的是:
https://login.live.com/oauth20_authorize.srf?client_id=00[...]00&scope=wl.signin%20wl.offline_access%20onedrive.readonly&response_type=code&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf
但 OneDrive 服务器回答我:
Microsoft 帐户需要 JavaScript 才能登录。此 Web 浏览器不支持 JavaScript,或者脚本被阻止。要了解您的浏览器是否支持 JavaScript 或是否允许脚本,请参阅浏览器的在线帮助。
所以我在浏览器中尝试请求,OneDrive 服务器将我重定向到授权页面:
现在的问题是:是否有任何解决方法可以跳过手动授权?
谢谢, 阿提里奥
最佳答案
客户端需要身份验证提供程序才能检索身份验证 token 。根据您当前的平台,有几种方法可以做到这一点。
- 创建您自己的
IAuthenticationProvider
实现。身份验证提供程序负责在请求中设置身份验证 header 。以下是使用自定义身份验证提供程序创建客户端实例的方法:
var client = new OneDriveClient(appConfig, serviceInfoProvider: new ServiceInfoProvider(new CustomAuthenticationProvider()));
使用各种默认身份验证实现中的一种。看看 SDK authentication documentation查看可用的选项和示例。
如果您有刷新 token 并且只想执行静默身份验证流程,您可以使用
OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient
。这是一个例子:
var client = await OneDriveClient.GetSilentlyAuthenticatedMicrosoftAccountClient(clientId, returnUrl, scopes, refreshToken);
关于c# - OneDrive 客户端身份验证 (SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34331098/