linvi/tweetinvi

Trying to authenticate user throws exception due to missing "tweetinvi_auth_request_id"

cmatskas opened this issue · 0 comments

Hi,

I have a .NET 7 Razor Page app. When I try to authenticate a user I get the following exception:

ArgumentException: Could not extract the token id as 'tweetinvi_auth_request_id' was not part of the url (Parameter 'callbackUrl')
Tweetinvi.Auth.LocalAuthenticationRequestStore.ExtractAuthenticationRequestIdFromCallbackUrl(string callbackUrl)
Tweetinvi.Parameters.RequestCredentialsParameters.FromCallbackUrlAsync(string callbackUrl, IAuthenticationRequestStore authenticationRequestStore)
Program+<>c__DisplayClass0_0+<<<Main>$>b__0>d.MoveNext() in Program.cs
+
    var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(request.Request.QueryString.Value, _myAuthRequestStore);
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I can confirm that the tweetinvi_auth_request_id url parameter is set in the first part of the code. However, it's missing from the URL parameters returned from twitter in this line of code (second part)

var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(request.Request.QueryString.Value, _myAuthRequestStore);

My Twitter App settings are attached below:
image

This is the code (copy pasted from your docs):

In a Razor page

    public async Task<IActionResult> OnGet()
    {
        var appClient = new TwitterClient("Zd8enAyK2atWSRUZGDrCq6cL6", "dlsznioXBG2VqUPwbwWQUkZhMxCJRKc4JoDm7DlVkDSD4GeNdr");
        var authenticationRequestId = Guid.NewGuid().ToString();
        var redirectPath = Request.Scheme + "://" + Request.Host.Value + "/ValidateTwitterAuth";

        // Add the user identifier as a query parameters that will be received by `ValidateTwitterAuth`
        var redirectURL = _myAuthRequestStore.AppendAuthenticationRequestIdToCallbackUrl(redirectPath, authenticationRequestId);
        // Initialize the authentication process
        var authenticationRequestToken = await appClient.Auth.RequestAuthenticationUrlAsync(redirectURL);
        // Store the token information in the store
        await _myAuthRequestStore.AddAuthenticationTokenAsync(authenticationRequestId, authenticationRequestToken);
        // Redirect the user to Twitter
        return Redirect("/ValidateTwitterAuth");    
    }

An route to capture the response from Twitter in the middleware:

app.MapGet("/ValidateTwitterAuth", async (request) =>{
    var appClient = new TwitterClient("Zd8enAyK2atWSRUZGDrCq6cL6", "dlsznioXBG2VqUPwbwWQUkZhMxCJRKc4JoDm7DlVkDSD4GeNdr");
    IAuthenticationRequestStore _myAuthRequestStore;
    using (var scope = app.Services.CreateScope())
    {
        _myAuthRequestStore = scope.ServiceProvider.GetRequiredService<IAuthenticationRequestStore>();
    }
    // Extract the information from the redirection url
    var requestParameters = await RequestCredentialsParameters.FromCallbackUrlAsync(request.Request.QueryString.Value, _myAuthRequestStore);
    // Request Twitter to generate the credentials.
    var userCreds = await appClient.Auth.RequestCredentialsAsync(requestParameters);

    // Congratulations the user is now authenticated!
    var userClient = new TwitterClient(userCreds);
    var user = await userClient.Users.GetAuthenticatedUserAsync();
});

Any help would be greatly appreciated