服务器未找到回调URL

时间:2017-01-31 22:00:30

标签: c# .net asp.net-web-api oauth oauth-2.0

我正在尝试在我的WebAPI应用程序中使用Fitbit实现OAuth2。我能够向fitbit api提出初始请求。但当它回来时服务器我收到一个错误,说我找不到回调网址

OAuthController

[HttpPost]
public async Task<HttpResponseMessage> Authorize(UserAuthRequestDTO request)
{
   if (string.IsNullOrEmpty(request.PatientID) || string.IsNullOrEmpty(request.Provider))
      Request.CreateResponse(HttpStatusCode.BadRequest, ErrorLookup.GetErrorMessage("invalid_input"), Configuration.Formatters.JsonFormatter);
      var userId = User.Identity.GetUserId();
      if (userId == null)
          return Request.CreateResponse(HttpStatusCode.BadRequest, ErrorLookup.GetErrorMessage("invalid_token"), Configuration.Formatters.JsonFormatter);
            var accUser = await GetUserById(userId);
      _currentUser = AccountUtils.GetOrgAndUserInfo(accUser);

      var callbackUrl = $"{Request.RequestUri.GetLeftPart(UriPartial.Authority)}/oauth2/callback";
      IOAuthHandler handler;
      switch (request.Provider)
      {
          case "Fitbit":
              handler = new FitbitHandler(callbackUrl);
              break;
          case "Withings":
              handler = new WithingsHandler(callbackUrl);
              break;
          default:
              return Request.CreateResponse(HttpStatusCode.BadRequest, ErrorLookup.GetErrorMessage("invalid_input"), Configuration.Formatters.JsonFormatter);
      }
    var authorizationUrl = handler.RequestUserAuthorizationUrl(request.PatientID,_currentUser.Org);
    return Request.CreateResponse(HttpStatusCode.OK, authorizationUrl);
}


[HttpPost]
public async Task<HttpResponseMessage> Callback(UserAuthDTO request)
{
    if (string.IsNullOrEmpty(request.PatientID))
        Request.CreateResponse(HttpStatusCode.BadRequest, ErrorLookup.GetErrorMessage("invalid_input"), Configuration.Formatters.JsonFormatter);
    var userId = User.Identity.GetUserId();
    if (userId == null)

1 个答案:

答案 0 :(得分:1)

您正在将回调网址设置为

...oauth2/callback?code=3aa6e9e....

但在您的行动中,您的路线是

...api/oauth2/callback

您的定义中缺少"api"