henrikfroehling / TraktApiSharp

DEPRECATED!!! A .NET library for the Trakt.tv API.
MIT License
31 stars 4 forks source link

OAuth Error: TraktAuthorizationException: Unauthorized - OAuth must be provided #104

Closed scottkuhl closed 6 years ago

scottkuhl commented 6 years ago

Trakt API Sharp Version: 0.11.0

When trying to access resources in the Trakt API that require OAuth authentication I am getting the following error:

TraktApiSharp.Exceptions.TraktAuthorizationException: Unauthorized - OAuth must be provided

at TraktApiSharp.Requests.Base.TraktRequest'3.d71.MoveNext() in C:\projects\traktapisharp\Source\Lib\TraktApiSharp\Requests\Base\TraktRequest.cs:line 368 at TraktApiSharp.Requests.Base.TraktRequest'3.d8.MoveNext() in C:\projects\traktapisharp\Source\Lib\TraktApiSharp\Requests\Base\TraktRequest.cs:line 50 at TraktApiSharp.Modules.TraktBaseModule.d4`2.MoveNext() in C:\projects\traktapisharp\Source\Lib\TraktApiSharp\Modules\TraktBaseModule.cs:line 0 at TraktApiSharp.Modules.TraktUsersModule.d11.MoveNext() in C:\projects\traktapisharp\Source\Lib\TraktApiSharp\Modules\TraktUsersModule.cs:line 335 at Conduktor.Controllers.HomeController.d__2.MoveNext() in C:\Users\scott\source\repos\Conduktor\Conduktor\Controllers\HomeController.cs:line 58

Here is the code in a ASP.NET Core 2.0 Controller that can produce it:

public async Task<IActionResult> Index(string code = "")
{
    var client = new TraktClient(ClientId, ClientSecret);
    var authorization = new TraktAuthorization();

    if (code != string.Empty)
    {
        authorization = await client.OAuth.GetAuthorizationAsync(code);
        Response.Cookies.Append("AccessToken", authorization.AccessToken);
        Response.Cookies.Append("RefreshToken", authorization.RefreshToken);
    }
    else
    {
        var authFound = Request.Cookies.TryGetValue("AccessToken", out var accessToken);
        var refreshFound = Request.Cookies.TryGetValue("RefreshToken", out var refreshToken);

        if (!authFound || !refreshFound)
        {
            ViewBag.AuthorizationUrl = client.OAuth.CreateAuthorizationUrl();
            return View();
        }
        else
        {
            var revoked = await client.Authentication.CheckIfAccessTokenWasRevokedOrIsNotValidAsync(accessToken);

            if (revoked)
            {
                authorization = await client.DeviceAuth.RefreshAuthorizationAsync();
                Response.Cookies.Append("AccessToken", authorization.AccessToken);
                Response.Cookies.Append("RefreshToken", authorization.RefreshToken);
            }
            else
            {
                client = new TraktClient(ClientId, ClientSecret)
                {
                    Authorization = TraktAuthorization.CreateWith(accessToken, refreshToken)
                };
            }
        }
    }

    var lists = await client.Users.GetCustomListsAsync("me");
    return View();
}
henrikfroehling commented 6 years ago

@scottkuhl The reason for the exception is that the request itself has an optional OAuth requirement and the library ignores this fact.

A workaround would be to set client.Configuration.ForceAuthorization to true.

scottkuhl commented 6 years ago

Thank you, I will give that a shot and see if it fixes it.

scottkuhl commented 6 years ago

That did the trick. You can close the issue. Thanks!