Skip to content

Commit b6834e1

Browse files
committed
AuthService async/await
1 parent f9b673c commit b6834e1

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

GoogleApis.Blazor/Auth/AuthService.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
using Microsoft.AspNetCore.Components;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.JSInterop;
5-
using System;
6-
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Net.Http;
95
using System.Net.Http.Headers;
106
using System.Text;
117
using System.Text.Json;
12-
using System.Threading.Tasks;
138
using System.Web;
149

1510
namespace GoogleApis.Blazor.Auth
@@ -69,7 +64,7 @@ public async Task RequestAuthorizationCode(string clientId, List<Scope> scopes,
6964
/// <param name="clientSecret"></param>
7065
/// <param name="redirectUrl"></param>
7166
/// <returns></returns>
72-
public string AuthorizeCredential(string authorizationCode, string clientId, string clientSecret, string redirectUrl)
67+
public async Task<string> AuthorizeCredential(string authorizationCode, string clientId, string clientSecret, string redirectUrl)
7368
{
7469
string encodedAuthorizationCode = HttpUtility.UrlEncode(authorizationCode);
7570
string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl);
@@ -78,9 +73,9 @@ public string AuthorizeCredential(string authorizationCode, string clientId, str
7873
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
7974
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");
8075

81-
var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result;
76+
var request = await client.PostAsync("https://oauth2.googleapis.com/token", content);
8277

83-
return request.Content.ReadAsStringAsync().Result;
78+
return await request.Content.ReadAsStringAsync();
8479
}
8580

8681
/// <summary>
@@ -91,17 +86,17 @@ public string AuthorizeCredential(string authorizationCode, string clientId, str
9186
/// <param name="clientSecret"></param>
9287
/// <param name="redirectUrl"></param>
9388
/// <returns></returns>
94-
public string RefreshAccessToken(string refreshToken, string clientId, string clientSecret, string redirectUrl)
89+
public async Task<string> RefreshAccessToken(string refreshToken, string clientId, string clientSecret, string redirectUrl)
9590
{
9691
string encodedRedirectUrl = HttpUtility.UrlEncode(redirectUrl);
9792

9893
var client = HttpClientFactory.CreateClient();
9994
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
10095
var content = new StringContent($"refresh_token={refreshToken}&client_id={clientId}&client_secret={clientSecret}&redirect_uri={encodedRedirectUrl}&scope=&grant_type=refresh_token", Encoding.UTF8, "application/x-www-form-urlencoded");
10196

102-
var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result;
97+
var request = await client.PostAsync("https://oauth2.googleapis.com/token", content);
10398

104-
var result = request.Content.ReadAsStringAsync().Result;
99+
var result = await request.Content.ReadAsStringAsync();
105100

106101
var jsonResult = JsonDocument.Parse(result);
107102
string accessToken = jsonResult.RootElement.GetProperty("access_token").ToString();
@@ -114,7 +109,7 @@ public string RefreshAccessToken(string refreshToken, string clientId, string cl
114109
/// </summary>
115110
/// <param name="refreshToken"></param>
116111
/// <returns></returns>
117-
public string RefreshAccessToken(string refreshToken)
112+
public async Task<string> RefreshAccessToken(string refreshToken)
118113
{
119114
string clientId = GetClientId();
120115
string clientSecret = GetClientSecret();
@@ -124,9 +119,9 @@ public string RefreshAccessToken(string refreshToken)
124119
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
125120
var content = new StringContent($"refresh_token={refreshToken}&client_id={clientId}&client_secret={clientSecret}&redirect_uri={encodedRedirectUrl}&scope=&grant_type=refresh_token", Encoding.UTF8, "application/x-www-form-urlencoded");
126121

127-
var request = client.PostAsync("https://oauth2.googleapis.com/token", content).Result;
122+
var request = await client.PostAsync("https://oauth2.googleapis.com/token", content);
128123

129-
var result = request.Content.ReadAsStringAsync().Result;
124+
var result = await request.Content.ReadAsStringAsync();
130125

131126
var jsonResult = JsonDocument.Parse(result);
132127
string accessToken = jsonResult.RootElement.GetProperty("access_token").ToString();
@@ -198,14 +193,14 @@ public bool IsAccessTokenExpired(string contentResult)
198193
/// </summary>
199194
/// <param name="accessToken"></param>
200195
/// <returns></returns>
201-
public string GetUserMail(string accessToken)
196+
public async Task<string> GetUserMail(string accessToken)
202197
{
203198
var client = HttpClientFactory.CreateClient();
204199
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
205200
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
206-
var result = client.GetAsync("https://www.googleapis.com/userinfo/v2/me").Result;
201+
var result = await client.GetAsync("https://www.googleapis.com/userinfo/v2/me");
207202

208-
var jsonResult = JsonDocument.Parse(result.Content.ReadAsStringAsync().Result);
203+
var jsonResult = JsonDocument.Parse(await result.Content.ReadAsStringAsync());
209204
string email = jsonResult.RootElement.GetProperty("email").ToString();
210205

211206
return email;
@@ -232,12 +227,12 @@ public string GetValueFromCredential(string credential, CredentialValueType cred
232227
/// </summary>
233228
/// <param name="accessToken"></param>
234229
/// <returns></returns>
235-
public string GetAccessTokenDetails(string accessToken)
230+
public async Task<string> GetAccessTokenDetails(string accessToken)
236231
{
237232
var client = HttpClientFactory.CreateClient();
238-
var result = client.GetAsync($"https://oauth2.googleapis.com/tokeninfo?access_token={accessToken}").Result;
239-
240-
return result.Content.ReadAsStringAsync().Result;
233+
var result = await client.GetAsync($"https://oauth2.googleapis.com/tokeninfo?access_token={accessToken}");
234+
if (result.StatusCode != System.Net.HttpStatusCode.OK) return null;
235+
return await result.Content.ReadAsStringAsync();
241236
}
242237

243238
}

0 commit comments

Comments
 (0)