Skip to content

Commit d0f0673

Browse files
committed
Add project files.
1 parent ae848ea commit d0f0673

19 files changed

Lines changed: 994 additions & 0 deletions
83.7 KB
Binary file not shown.

.vs/GoogleApis.Blazor/v17/.suo

32.5 KB
Binary file not shown.

GoogleApis.Blazor.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32421.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GoogleApis.Blazor", "GoogleApis.Blazor\GoogleApis.Blazor.csproj", "{7460C3F8-290E-4920-AE79-857E37DA1BBD}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7460C3F8-290E-4920-AE79-857E37DA1BBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7460C3F8-290E-4920-AE79-857E37DA1BBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7460C3F8-290E-4920-AE79-857E37DA1BBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7460C3F8-290E-4920-AE79-857E37DA1BBD}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {D439B68B-2B00-47D1-9D95-AA571BE5853D}
24+
EndGlobalSection
25+
EndGlobal

GoogleApis.Blazor/AllEnums.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.ComponentModel;
2+
3+
namespace GoogleApis.Blazor
4+
{
5+
public static class EnumExtensions
6+
{
7+
public static string ToDescriptionString(this Enum val)
8+
{
9+
var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
10+
11+
return attributes.Length > 0
12+
? attributes[0].Description
13+
: val.ToString();
14+
}
15+
}
16+
17+
public enum Scope
18+
{
19+
[Description("https://www.googleapis.com/auth/userinfo.email")]
20+
OAuth2Email,
21+
[Description("https://www.googleapis.com/auth/userinfo.profile")]
22+
OAuth2Profile,
23+
[Description("https://www.googleapis.com/auth/analytics")]
24+
Analytics,
25+
[Description("https://www.googleapis.com/auth/analytics.readonly")]
26+
AnalyticsReadonly,
27+
[Description("https://www.googleapis.com/auth/calendar")]
28+
Calendar,
29+
[Description("https://www.googleapis.com/auth/calendar.readonly")]
30+
CalendarReadonly,
31+
[Description("https://www.googleapis.com/auth/calendar.events")]
32+
CalendarEvents,
33+
[Description("https://www.googleapis.com/auth/calendar.events.readonly")]
34+
CalendarEventsReadonly,
35+
[Description("https://www.googleapis.com/auth/drive")]
36+
Drive,
37+
[Description("https://www.googleapis.com/auth/drive.metadata")]
38+
DriveReadonly,
39+
[Description("https://www.googleapis.com/auth/drive.file")]
40+
DriveFile,
41+
[Description("https://www.googleapis.com/auth/drive.appdata")]
42+
DriveAppData,
43+
[Description("https://www.googleapis.com/auth/drive.metadata")]
44+
DriveMetaData,
45+
[Description("https://www.googleapis.com/auth/drive.metadata.readonly")]
46+
DriveMetaDataReadonly,
47+
}
48+
49+
public enum CredentialValueType
50+
{
51+
[Description("access_token")]
52+
AccessToken,
53+
[Description("refresh_token")]
54+
RefreshToken,
55+
[Description("scope")]
56+
Scope,
57+
[Description("token_type")]
58+
TokenType,
59+
[Description("expires_in")]
60+
ExpiresIn,
61+
}
62+
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.JSInterop;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net.Http.Headers;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Threading.Tasks;
11+
using System.Web;
12+
13+
namespace GoogleApis.Blazor.Auth
14+
{
15+
public class AuthService
16+
{
17+
[Inject] IJSRuntime JSRuntime { get; set; }
18+
[Inject] IHttpClientFactory HttpClientFactory { get; set; }
19+
20+
public AuthService(IJSRuntime jsRuntime, IHttpClientFactory httpClientFactory)
21+
{
22+
JSRuntime = jsRuntime;
23+
HttpClientFactory = httpClientFactory;
24+
}
25+
26+
/// <summary>
27+
/// oAuth2 Step 1. Opens "Select Google Account" page in a new tab and return the value into redirectUrl.
28+
/// </summary>
29+
/// <param name="clientId"></param>
30+
/// <param name="scope"></param>
31+
/// <param name="redirectUrl"></param>
32+
/// <returns></returns>
33+
public async Task RequestAuthorizationCode(string clientId, Scope scope, string redirectUrl)
34+
{
35+
string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl);
36+
await JSRuntime.InvokeAsync<object>("open", $"https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?response_type=code&client_id={clientId}&scope={scope.ToDescriptionString()}&redirect_uri={encodedRedirectUrl}&flowName=GeneralOAuthFlow&access_type=offline&prompt=consent", "_blank");
37+
38+
}
39+
40+
/// <summary>
41+
/// oAuth2 Step 2. Get credentials with given authorizationCode. Returns string content.
42+
/// </summary>
43+
/// <param name="authorizationCode"></param>
44+
/// <param name="clientId"></param>
45+
/// <param name="clientSecret"></param>
46+
/// <param name="redirectUrl"></param>
47+
/// <returns></returns>
48+
public string AuthorizeCredential(string authorizationCode, string clientId, string clientSecret, string redirectUrl)
49+
{
50+
string encodedAuthorizationCode = HttpUtility.UrlEncode(authorizationCode);
51+
string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl);
52+
53+
var client = HttpClientFactory.CreateClient();
54+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
55+
var content = new StringContent($"code={encodedAuthorizationCode}&client_id={clientId}&client_secret={clientSecret}&redirect_uri={encodedRedirectUrl}&scope=&grant_type=authorization_code", Encoding.UTF8, "application/x-www-form-urlencoded");
56+
57+
var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result;
58+
59+
return request.Content.ReadAsStringAsync().Result;
60+
}
61+
62+
public string GetValueFromCredential(string credential, CredentialValueType credentialValueType)
63+
{
64+
string accessToken = string.Empty;
65+
66+
var jsonResult = JsonDocument.Parse(credential);
67+
accessToken = jsonResult.RootElement.GetProperty(credentialValueType.ToDescriptionString()).ToString();
68+
69+
return accessToken;
70+
}
71+
72+
}
73+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.AspNetCore.Components;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace GoogleApis.Blazor.Calendar
9+
{
10+
public class CalendarService
11+
{
12+
[Inject] IHttpClientFactory HttpClientFactory { get; set; }
13+
14+
public CalendarService(IHttpClientFactory httpClientFactory)
15+
{
16+
HttpClientFactory = httpClientFactory;
17+
}
18+
19+
/// <summary>
20+
/// Get all calendars that authenticated user has.
21+
/// </summary>
22+
/// <param name="accessToken"></param>
23+
/// <returns></returns>
24+
public string GetCalendars(string accessToken)
25+
{
26+
var client = HttpClientFactory.CreateClient();
27+
28+
var result = client.GetAsync("https://www.googleapis.com/calendar/v3/users/me/calendarList?access_token=" + accessToken).Result;
29+
30+
if (!result.IsSuccessStatusCode)
31+
{
32+
return "error";
33+
}
34+
35+
return result.Content.ReadAsStringAsync().Result;
36+
}
37+
38+
/// <summary>
39+
/// Get all calendars that authenticated user has.
40+
/// </summary>
41+
/// <param name="accessToken"></param>
42+
/// <returns></returns>
43+
public string GetCalendar(string accessToken, string calendarId)
44+
{
45+
var client = HttpClientFactory.CreateClient();
46+
47+
var result = client.GetAsync($"https://www.googleapis.com/calendar/v3/users/me/calendarList/{calendarId}?access_token=" + accessToken).Result;
48+
49+
if (!result.IsSuccessStatusCode)
50+
{
51+
return "error";
52+
}
53+
54+
return result.Content.ReadAsStringAsync().Result;
55+
}
56+
57+
}
58+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>disable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.Components" Version="6.0.4" />
11+
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
12+
<PackageReference Include="Microsoft.JSInterop" Version="6.0.4" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// <autogenerated />
2+
using System;
3+
using System.Reflection;
4+
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
// Runtime Version:4.0.30319.42000
5+
//
6+
// Changes to this file may cause incorrect behavior and will be lost if
7+
// the code is regenerated.
8+
// </auto-generated>
9+
//------------------------------------------------------------------------------
10+
11+
using System;
12+
using System.Reflection;
13+
14+
[assembly: System.Reflection.AssemblyCompanyAttribute("GoogleApis.Blazor")]
15+
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
16+
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
17+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
18+
[assembly: System.Reflection.AssemblyProductAttribute("GoogleApis.Blazor")]
19+
[assembly: System.Reflection.AssemblyTitleAttribute("GoogleApis.Blazor")]
20+
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
21+
22+
// Generated by the MSBuild WriteCodeFragment class.
23+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8041f0a2091302c29878f7980c16972e55a376f7

0 commit comments

Comments
 (0)