我正在尝试在我的应用程序中使用REST API,但我无法让它工作 .
我成功获得了授权码并保存了它 . (获得 wl.offline_access
许可)
我想登录OneDrive并在后台任务中上传文件,所以我需要刷新令牌 . 然后我进行POST调用以获取刷新令牌,但它不起作用 . 我究竟做错了什么?
这是我的代码 .
var data = "client_id=CLIENT_ID&redirect_uri=https://login.live.com/oauth20_desktop.srf&client_secret=CLIENT_SCRET&code=AUTHORIZATION_CODE&grant_type=authorization_code";
data = Uri.EscapeDataString(data);
var webClient = new WebClient
{
Headers =
{
[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"
}
};
webClient.UploadStringCompleted += (sender, e) =>
{
// e.Result throws an exception of type 'System.Reflection.TargetInvocationException'
// e.Error = {"The remote server returned an error: NotFound."}
// e.Error.Response.StatusCode = BadRequest
// e.Error.Response.Headers = {Cache-Control: no-store
// Pragma: no-cache
// Content-Length: 113
// Content-Type: application/json
// Server: Microsoft-IIS/8.5
// X-WLID-Error: 0x80049D58
// X-Content-Type-Options: nosniff
// Date: Sat, 31 Oct 2015 20:22:04 GMT
// Connection: close}
};
var uri = new Uri("https://login.live.com/oauth20_token.srf",UriKind.Absolute);
webClient.UploadStringAsync(uri, "POST", data);
1 回答
在代码流期间检索的代码不是't valid for an extended period of time. What you' ll想要做的是在检索代码之后立即存储上面POST调用返回的刷新令牌 . 然后,您可以使用该刷新令牌脱机检索访问令牌 . 刷新令牌有效期为一年,每当您检索新的访问令牌时,您都会收到一个新令牌 . OneDrive code flow documentation有更多信息 .