【发布时间】:2023-06-04 06:27:01
【问题描述】:
我正在使用 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 辅助角色访问 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 - 未能为用户检索有效的身份验证令牌。
有什么建议吗?
谢谢。
更新 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 服务器将我重定向到授权页面:
现在的问题是:是否有任何解决方法可以跳过手动授权?
谢谢你, 阿提利奥
【问题讨论】:
标签: c# authentication sdk onedrive