Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Urban committed May 14, 2023
1 parent 6706c32 commit d5511c0
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,94 @@ https://developers.google.com/identity/protocols/OAuth2UserAgent

2. Add `builder.Services.AddScoped<IReminders,Reminders>();`

3. Use it:

@page "/"

@using System.Text.RegularExpressions;

@inject NavigationManager navigationManager
@inject IReminders googleReminders

<h1>Reminders:</h1>

<div>@((MarkupString)innerHTML)</div>

@code {
private const string YOUR_CLIENT_ID = "REPLACE_THIS_VALUE";
private const string YOUR_REDIRECT_URI = "REPLACE_THIS_VALUE";

Dictionary<string, string> oauth2Params = new Dictionary<string, string>();
string innerHTML = "";

protected override async Task OnInitializedAsync()
{
string fragment = new Uri(navigationManager.Uri).Fragment;

if (fragment.Length > 0)
{
string fragmentString = fragment.Substring(1);

// Parse query string to see if page request is coming from OAuth 2.0 server.
Regex regex = new Regex("([^&=]+)=([^&]*)");

foreach (Match match in regex.Matches(fragmentString))
{
oauth2Params[Uri.UnescapeDataString(match.Groups[1].Value)] = Uri.UnescapeDataString(match.Groups[2].Value);
}
}

await SendRequest();
}

async Task SendRequest()
{
if (oauth2Params != null && oauth2Params.TryGetValue("access_token", out string? accessToken))
{
List<Reminder>? reminders = await googleReminders.ListReminders(accessToken, 10);

if (reminders != null)
{
foreach (Reminder reminder in reminders)
{
innerHTML += "<p>" + reminder + "</p>";
}
}
else
{
// Token invalid, so prompt for user permission.
OAuth2SignIn();
}
}
else
{
OAuth2SignIn();
}
}

void OAuth2SignIn()
{
// Google's OAuth 2.0 endpoint for requesting an access token
string oauth2Endpoint = "https://accounts.google.com/o/oauth2/v2/auth";

// Parameters to pass to OAuth 2.0 endpoint.
Dictionary<string, string> queryParams = new Dictionary<string, string>
{
{ "client_id", YOUR_CLIENT_ID },
{ "redirect_uri", YOUR_REDIRECT_URI },
{ "scope", "https://www.googleapis.com/auth/reminders" },
{ "include_granted_scopes", "true" },
{ "response_type", "token" }
};

// Build the query string from the parameters
string queryString = string.Join("&", queryParams.Select(p => $"{p.Key}={Uri.EscapeDataString(p.Value)}"));

// Navigate to the OAuth 2.0 endpoint in a new window.
navigationManager.NavigateTo($"{oauth2Endpoint}?{queryString}", true);
}
}

## Version history:

- 0.0.0.1
Expand Down

0 comments on commit d5511c0

Please sign in to comment.