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异常,其Error属性指出: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服务器将我重定向到授权页面:
![enter image description here](https://istack.dev59.com/CYbnz.webp)
谢谢, Attilio