为什么使用Brightspace API时会收到403响应?

问题描述 投票:0回答:1

我正在尝试使用Brightspace API,但收到403(禁止访问)响应。

我已经使用“管理可扩展性(/ d2l / lp / extensibility / home)”页面注册了我的应用程序,我已经从API Test Tool中生成了一个用户ID和密钥。

使用所有这些,我已经在项目中安装了D2L.Extensibility.AuthSdk NuGet软件包。然后,在相关的类中,我为UserContext创建了一个属性,并按如下所示在构造函数中对其进行了初始化:

_d2LUserContext = new D2LAppContextFactory()
    .Create(OrionConfiguration.D2LApplicationId, OrionConfiguration.D2LApiKey)
    .CreateUserContext(
        "censored user id",
        "censored user key",
        new HostSpec("https", OrionConfiguration.D2LUrl.Substring(8), 443)
    );

注意:

  1. .Substring(8)是因为D2LUrl包含URL方案
  2. 用户ID和密钥是今天早上生成的,因此尚未过期

然后,我正在尝试调用API。用于此目的的代码分为几种方法。

private string AuthParam(string path, string method)
{
    return _d2LUserContext
        .CreateAuthenticatedTokens($"/d2l/api/lp/1.2{path}", method)
        .Select(tuple => $"{tuple.Item1}={tuple.Item2}")
        .Aggregate((acc, p) => $"{acc}&{p}");
}

public Task<UserResponse> CreateUser(UserRequest userRequest)
{
    const string path = "/users";
    return _httpUtils.Post<UserResponse>($"{path}/?{AuthParam(path, "POST")}", userRequest);
}

[UserRequestthe model the API expects的POC#O(普通旧C#对象)版本。

[这是HttpUtils类中的相关方法-这是我编写的HttpClient的包装,以摆脱其他类中的一些样板。

internal async Task<T> Post<T>(string route, dynamic body)
{
    var response = await _httpClient.PostAsync(
        _baseUrl + route, 
        new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json")
    );

    _logger.LogInformation($"POST request to {route}");
    _logger.LogInformation(await response.Content.ReadAsStringAsync());
    return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}

现在,将所有内容放在一起,当我尝试调试这些方法时,我在POST请求后的行设置了一个断点,我们可以看到得到了403403 error in debugger

我想知道为什么会这样。生成密钥和ID的用户是超级管理员,因此这不是权限问题。

desire2learn
1个回答
0
投票

实际的API route in question带有斜杠,因此需要将斜杠放入生成身份验证令牌/签名的方法中。在我看来,您正在执行的操作是传递不带此斜杠的path,然后将其放入请求中,这是您附加查询参数时的副作用,因此您正在生成一个一个API路由的身份验证签名,然后在调用中使用另一个。

Brightspace API路由对此斜杠非常敏感,但不幸的是,它们并不总是在路由中始终干净或一致地应用。

© www.soinside.com 2019 - 2024. All rights reserved.